热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【CentOS7架构23】,设置nginx用户认证#180104

2019独角兽企业重金招聘Python工程师标准hellopasswdnginx用户认证viusrlocalnginxconfvhosttest.com.confserver

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

hellopasswd


nginx用户认证
  • vi /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; serever_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com;

    location /{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;}

    }

  • yum install -y httpd

  • htpasswd -c /usr/local/nginx/conf/htpasswd aming

  • t && -s reload #测试配置并重新加载

[root@localhost default]# cd /usr/local/nginx/conf/vhost
[root@localhost vhost]# vi test.com.conf1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location /9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }

[root@localhost vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd user
New password:
Re-type new password:
Adding password for user user
[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd
user:$apr1$NVdIvb1f$14A08nppYo/PwUYjQRD6O.

[root@localhost vhost]# /usr/local/nginx/sbin/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 vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com



401 Authorization Required



nginx/1.4.7



[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com



404 Not Found



nginx/1.4.7


[root@localhost vhost]# ls /data/wwwroot/test.com
ls: cannot access /data/wwwroot/test.com: No such file or directory
[root@localhost vhost]# mkdir /data/wwwroot/test.com
[root@localhost vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com
test.com

若在某一目录下实现用户认证,如:test.com/admin/,则需将根目录/改为/admin/

[root@localhost vhost]# vi test.com.conf 1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location /admin/9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }[root@localhost vhost]# /usr/local/nginx/sbin/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 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com
test.com
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# echo "hello test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin/
hello test.com admin dir

以上为针对目录,若要针对当个文件,则需要匹配

[root@localhost vhost]# vi test.com.conf 1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location ~(.*)admin.php$9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }
[root@localhost vhost]# /usr/local/nginx/sbin/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 vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# vi /data/wwwroot/test.com/admin.php1 [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin/
hello test.com admin dir
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin.php



401 Authorization Required



nginx/1.4.7



[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin.php

修改于 180104


转:https://my.oschina.net/hellopasswd/blog/1602459



推荐阅读
author-avatar
love小蕾XM_578
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有