Nginx-四层访问控制,认证,自定义错误界面,自定义访问日志路径
Nginx 四层访问控制
访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制。
location /about { alias /data/nginx/html/pc; index index.html; deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 2001:0db8::/32; deny all; #先允许小部分,再拒绝大部分 }
Nginx账户认证功能
[root@s2 ~]# yum install httpd-tools -y #Centos [root@s2 ~]# apt install apache2-utils #Ubuntu [root@s2 ~]# htpasswd -cbm /apps/nginx/conf/.htpasswd user1 123456 Adding password for user user1 [root@s2 ~]# htpasswd -bm /apps/nginx/conf/.htpasswd user2 123456 Adding password for user user2 [root@s2 ~]# tail /apps/nginx/conf/.htpasswd user1:$apr1$Rjm0u2Kr$VHvkAIc5OYg.3ZoaGwaGq/ user2:$apr1$nIqnxoJB$LR9W1DTJT.viDJhXa6wHv. [root@s2 ~]# vim /apps/nginx/conf/conf.d/pc.conf location = /login/ { root /data/nginx/html/pc; index index.html; auth_basic "login password"; auth_basic_user_file /apps/nginx/conf/.htpasswd; } 重启Nginx并访问测试
自定义错误页面
listen 80; server_name www.magedu.net; error_page 500 502 503 504 404 /error.html; location = /error.html { root html; } 重启nginx并访问不存在的页面进行测试
自定义访问日志
[root@s2 ~]# mkdir /data/nginx/logs listen 80; server_name www.magedu.net; error_page 500 502 503 504 404 /error.html; #默认目录下面创建error.html页面 access_log /data/nginx/logs/www-magedu-net_access.log; //也可以放在location下面,记录某个uri的日志。一个server可以有多个location error_log /data/nginx/logs/www-magedu-net_error.log; location = /error.html { root html; } 重启nginx并访问不存在的页面进行测试并验证是在指定目录生成新的日志文件
本文来自博客园,作者:不会跳舞的胖子,转载请注明原文链接:https://www.cnblogs.com/rtnb/p/15780053.html

浙公网安备 33010602011771号