首页 > 其他 > 详细

nginx访问控制,用户认证

时间:2021-06-01 00:32:50      阅读:26      评论:0      收藏:0      [点我收藏+]

访问控制

用于location段
allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny:设定禁止哪台或哪些主机访问,多个参数间用空格隔开
示例:

allow 192.168.170.132/32 192.168.170.133/32;
deny all;

 

 

 

拒绝本机访问nginx状态页面

        location /status {
            stub_status on;
            deny 192.168.170.1;
        }

技术分享图片

 

 

 

 

基于用户认证

 

auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"

//user_auth_file内容格式
username:password

 

这里的密码为加密后的密码串,建议用htpasswd来创建此文件:

htpasswd -c -m /path/to/.user_auth_file USERNAME

 

授权用户

[root@localhost ssl]# yum install -y httpd-tools

//tom账户要之前没有创建的
[root@localhost conf]# htpasswd -c -m .user_auth_file  tom
New password: 
Re-type new password: 
Adding password for user tom
[root@localhost conf]# ls -a | grep .user_auth_file
.user_auth_file
[root@localhost conf]# cat .user_auth_file 
tom:$apr1$OhUzbIS3$f1MpCShCyvCYtMUIn6BMD1

 

配置(必须要用绝对路径)

[root@localhost conf]# vim nginx.conf
        location /status {
            stub_status on;
            auth_basic "欢迎查看";
            auth_basic_user_file "/usr/local/nginx/conf/.user_auth_file";
        }


[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# nginx -s reload

 

 

 

访问测试

技术分享图片

 

 

 

技术分享图片

 

 

技术分享图片

 

nginx访问控制,用户认证

原文:https://www.cnblogs.com/meijianbiao/p/14833600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!