server { listen 80; server_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; } }
location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/ [root@hanfeng vhost]#
[root@hanfeng vhost]# ls aaa.com.conf [root@hanfeng vhost]# vim test.com.conf 添加如下內容 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / //表示全站,都須要進行用戶認證 #location /admin // 這個地方只要加上」 /admin 」 就變成 針對這個站點的「admin」 這個目錄須要用戶認證 #location ~ admin.php //若是把這行這樣寫,就會變成,匹配 「 admin.php 」這個頁面的時候才須要用戶認證 { auth_basic "Auth"; //定義用戶認證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用戶名密碼文件 } } 保存退出
/usr/local/apache2.4/bin/htpasswd
yum install -y httpd
[root@hanfeng vhost]# yum install -y httpd 在yum安裝後,能夠直接使用htpasswd命令
[root@hanfeng vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd hanfeng New password: //密碼hanfeng Re-type new password: Adding password for user hanfeng [root@hanfeng vhost]#
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/ [root@hanfeng vhost]#
[root@hanfeng vhost]# htpasswd /usr/local/nginx/conf/htpasswd gurui New password: Re-type new password: Adding password for user gurui [root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/ gurui:$apr1$mqc2Dgwa$qVvurqGN6gj8hX3tEpQ6j/ [root@hanfeng vhost]#
[root@hanfeng 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@hanfeng vhost]#
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hanfeng vhost]#
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hanfeng vhost]# curl -x127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.12.1 Date: Wed, 03 Jan 2018 16:52:23 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth" [root@hanfeng vhost]#
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hanfeng vhost]#
[root@hanfeng vhost]# mkdir /data/wwwroot/test.com [root@hanfeng vhost]# echo 「test.com」>/data/wwwroot/test.com/index.html [root@hanfeng vhost]#
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com 「test.com」 [root@hanfeng vhost]#
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/ <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]#
[root@hf-01 vhost]# vim test.com.conf 在location / 後加上admin/ 目錄 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } 保存退出
[root@hf-01 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@hf-01 vhost]#
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com 「test.com」 [root@hf-01 vhost]#
6.訪問test.com/admin/目錄php
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]#
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin [root@hf-01 vhost]#
[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/adm in/index.html [root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@hf-01 vhost]#
[root@hf-01 vhost]# vim test.com.conf 在 location 後加~ admin.php便可 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location ~ admin.php { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } } 保存退出
[root@hf-01 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@hf-01 vhost]#
[root@hf-01 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hf-01 vhost]#
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin.php <html> <head><title>401 Authorization Required</title></head> <body bgcolor="white"> <center><h1>401 Authorization Required</h1></center> <hr><center>nginx/1.12.1</center> </body> </html> [root@hf-01 vhost]#