Nginx的状态统计、目录保护、访问控制

1.nginx的状态统计
#vim /usr/local/nginx/conf/nginx.conf
#在server块中添加:
location /nginx_status{
    stub_status on;
    #开启状态统计
    access_log ogg;
    #状态统计不记录日志
}

#验证:

2.目录保护

以保护状态统计页面为例,配置目录保护
(1)使用http服务的命令htpasswd进行用户和密码文件的创建

htpasswd -c /usr/local/nginx/html/htpasswd.nginx username

(2)在location块中添加如下两行:

auth_basic "Welcome to nginx_status.";
auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx;
location /nginx_status{
    stub_status on;
    access_log ogg;
    auth_basic "Welcome to nginx_status.";
    auth_basic_user_file /usr/local/nginx/html/htpasswd.nginx;
}

(3)重启nginx并再次访问统计页面

/usr/local/nginx/sbin/nginx -s reload

3.访问控制

#基于ip地址的身份验证
在location中添加

allow 10.0.0.102;
deny 10.0.0.0/24
#表示当前location指定的文件仅允许来自10.0.0.102的用户访问

#重启nginx,使用curl验证:

作者:ccku
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题或建议,请多多赐教,非常感谢。
原文地址:https://www.cnblogs.com/ccku/p/13531898.html