nginx网站服务(访问状态统计、虚拟主机应用、访问控制设置)
一:源码安装nginx
1.1:安装环境所需的软件包
[root@promote ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y
1.2:创建运行用户、组
[root@promote ~]# useradd -M -s /sbin/nologin nginx
1.3:解压软件包、编译安装
[root@promote ~]# cd /opt
[root@promote opt]# rz -E #用xshell连接虚拟机,可直接拖进来
rz waiting to receive.
[root@promote opt]# ls
nginx-1.12.2.tar.gz rh
[root@promote opt]# tar xzvf nginx-1.12.2.tar.gz
[root@promote opt]# cd nginx-1.12.2/
[root@promote nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@promote nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module #开启统计模块
[root@promote nginx-1.12.2]# make && make install
1.4:优化执行路径
[root@promote nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
1.5:编写状态管理脚本
[root@promote nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@promote nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@promote nginx-1.12.2]# chkconfig --add nginx
[root@promote nginx-1.12.2]# service nginx start
二:访问状态统计
- 修改nginx的配置文件
注意在编译安装时要加入统计模块
[root@promote conf]# vim nginx.conf
47 location /status {
48 stub_status on;
49 access_log off;
50 }
[root@promote conf]# service nginx stop
[root@promote conf]# service nginx start
- 访问网页加/status后缀
三:虚拟主机应用
3.1:基于域名的虚拟主机
- 安装DNS服务、解析两个域名www.kgc.com、www.benet.com
- 修改配置文件
[root@promote named]# vim /usr/local/nginx/conf/nginx.conf
#在文件末尾写入以下代码
server {
listen 80;
server_name www.kgc.com;
chatset utf-8;
access_log logs/www.kgc.com.access.log;
location / {
root /var/www/kgc;
index index.html index.php;
}
}
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.com.access.log;
location / {
root /var/www/benet;
index index.html index.php;
}
}
[root@promote named]# service nginx stop
[root@promote named]# service nginx start
- 创建站点目录
[root@promote ~]# cd /var
[root@promote var]# mkdir www
[root@promote var]# cd www/
[root@promote www]# mkdir kgc benet
[root@promote www]# ls
benet kgc
[root@promote www]# cd benet/
[root@promote benet]# vim index.html
<h1>this is benet web !</h1>
[root@promote benet]# cd ../kgc/
[root@promote kgc]# vim index.html
<h1>this is kgc web !</h1>
[root@promote kgc]# service nginx restart
- Windows客户端验证
要把DNS服务器地址改为解析www.kgc.com、www.benet.com这两个网站的主机地址
3.2:基于端口的虚拟主机
- 修改配置文件
[root@promote nginx]# vim conf/nginx.conf
server {
listen 192.168.209.138:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/kgc;
index index.html index.php;
}
}
server {
listen 192.168.209.138:8080;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/benet;
index index.html index.php;
}
}
[root@promote nginx]# service nginx restart
- windows客户端验证
3.3:基于IP的虚拟主机
- 增加双网卡
- 修改解析地址www.kgc.com(192.168.209.138)、www.benet.com(192.168.209.144)
- 修改配置文件
server {
listen 192.168.209.138:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
root /var/www/kgc;
index index.html index.php;
}
}
server {
listen 192.168.209.144:80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/benet;
index index.html index.php;
}
}
- 客户端验证
四:访问控制设置
4.1:基于客户端的访问控制
- 拒绝IP地址为192.168.209.180的主机访问
- 修改配置文件
server {
listen 192.168.209.138:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
deny 192.168.209.180; #增加此行
allow all; #增加此行
root /var/www/kgc;
index index.html index.php;
}
}
- 修改客户端的IP地址为192.168.209.180,访问www.kgc.com的网址
可以看到访问被拒绝
4.2:基于身份验证的访问
- 创建验证用户
需要先安装httpd的软件包才会有htpasswd的命令
[root@promote nginx]# htpasswd -c /usr/local/nginx/passwd.db harry
New password:
Re-type new password:
Adding password for user harry
[root@promote nginx]# chown nginx /usr/local/nginx/passwd.db
[root@promote nginx]# chmod 400 /usr/local/nginx/passwd.db
- 修改配置文件
server {
listen 192.168.209.138:80;
server_name www.kgc.com;
charset utf-8;
access_log logs/www.kgc.access.log;
location / {
auth_basic "secret"; #写入此行
auth_basic_user_file /usr/local/nginx/passwd.db; #写入此行
root /var/www/kgc;
index index.html index.php;
}
}
- 客户端验证
输入刚才创建的用户名与密码后可以正常访问网站