2019独角兽企业重金招聘Python工程师标准>>>
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
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com404 Not Found
[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
修改于 180104[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.php401 Authorization Required
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin.php