文章目录
一、访问状态统计二、访问控制基于授权的访问控制基于客户端的访问控制三、虚拟主机基于域名基于IP基于端口一、访问状态统计
首先使用/usr/local/nginx/sbin/nginx -V 来查看是否包含 --with-http_stub_status_module(开启访问状态统计模块)
修改/usr/local/nginx/conf/nginx.conf 配置文件 指定访问位置并添加stub_status配置
先备份配置文件,以防翻车还可以找回原有配置。
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bakvim /usr/local/nginx/conf/nginx.conf在合适的位置添加以下配置location /status {stub_status on;access_log off;}
二、访问控制
基于授权的访问控制
使用htpasswd命令,htpasswd是用于目录访问权限认证的一个工具。
-c 用于创建密码文件,入果文件已存在,会覆盖。
查看系统是否有该命令,若没有请安装httpd-tolls依赖包
[root@localhost ~]# which htpasswd/usr/bin/which: no htpasswd in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)[root@localhost ~]# yum -y install httpd-tools[root@localhost ~]# which htpasswd/usr/bin/htpasswd[root@localhost ~]#
创建密码文件
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db lisiNew password: //输入新密码Re-type new password: //确认新密码Adding password for user lisi //创建成功
修改属主交给ngxin管理,并给予400权限
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db [root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
修改nginx主配置文件,添加访问控制配置(在对应的location位置,想对哪个location访问控制,就在哪个location里配置)
我想对访问状态统计页面进行控制,配置如下
vim /usr/local/nginx/conf/nginx.conflocation /status {auth_basic "secret";autu_basic_user_file /usr/local/nginx/passwd.db;stub_status on;access_log off;}
重启服务,重新访问状态统计页面
基于客户端的访问控制
deny拒绝某个IP/IP段客户机访问
allow允许某个IP/IP段客户机访问
规则匹配顺序,从上往下,匹配即停止。
需求:添加一条规则拒绝192.168.177.130IP的主机访问
location / {//在此location位置配置规则root html;index index.html index.htm;deny 192.168.177.130; //拒绝访问IPallow all; //允许所有}
去IP为192.168.177.130的主机上测试访问
三、虚拟主机
三种方式:基于域名、基于IP、基于端口
基于域名:域名不同,IP地址相同,端口相同基于IP:域名不同,IP地址不同(可以是多个网卡,或者虚拟出一块网卡),端口相同基于端口:域名不同,IP地址相同,端口不同
基于域名
修改 /etc/hosts 文件,添加域名与IP的本地映射
echo "192.168.177.111 " >> /etc/hostsecho "192.168.177.111 " >> /etc/hosts
创建网页文件
mkdir -p /var/www/html/accpmkdir -p /var/www/html/benetecho "<h1> </h1>" >> /var/www/html/accp/index.htmlecho "<h1> </h1>" >> /var/www/html/benet/index.html
修改ngxin主配置文件:域名不同,IP地址相同,端口相同
vim /usr/local/nginx/conf/nginx.conf
server {36 listen 192.168.177.111:80;37 server_name ;38 charset utf-8;39 access_log logs/accp.access.log;40 location / {41 root /var/www/html/accp;42 index index.html index.htm;43 }44 error_page 500 502 503 504 /50x.html;45 location = /50x.html {46 root html;47 }48}49server {50 listen 192.168.177.111:80;51 server_name ;52 charset utf-8;53 access_log logs/benet.access.log;54 location / {55 root /var/www/html/benet;56 index index.html index.htm;57 }58 error_page 500 502 503 504 /50x.html;59 location = /50x.html {60 root html;61 }62}
重启服务,测试虚拟主机(基于域名)
基于IP
创建虚拟网卡
ifconfig ens33:0 192.168.177.200 netmask 255.255.255.0
修改nginx主配置文件 配置的IP地址为192.168.177.200
vim /usr/local/nginx/conf/nginx.conf
49server {50 listen 192.168.177.200:80;51 server_name ;52 charset utf-8;53 access_log logs/benet.access.log;54 location / {55 root /var/www/html/benet;56 index index.html index.htm;57 }58 error_page 500 502 503 504 /50x.html;59 location = /50x.html {60 root html;61 }62}
重启服务,测试虚拟主机(基于IP)
基于端口
修改nginx主配置文件 配置的IP地址为192.168.177.111 端口为8080
vim /usr/local/nginx/conf/nginx.conf
49server {50 listen 192.168.177.111:8080;51 server_name ;52 charset utf-8;53 access_log logs/benet.access.log;54 location / {55 root /var/www/html/benet;56 index index.html index.htm;57 }58 error_page 500 502 503 504 /50x.html;59 location = /50x.html {60 root html;61 }62}
重启服务,测试虚拟主机(基于端口)