新聞中心
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; } } yum install -y httpd htpasswd -c /usr/local/nginx/conf/htpasswd aming -t && -s reload //測試配置并重新加載 mkdir /data/wwwroot/test.com echo “test.com”>/data/wwwroot/test.com/index.html curl -x127.0.0.1:80 test.com -I//狀態(tài)碼為401說明需要驗(yàn)證 curl -uaming:passwd 訪問狀態(tài)碼變?yōu)?00 編輯windows的hosts文件,然后在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗 針對目錄的用戶認(rèn)證
location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } Nginx用戶認(rèn)證 首先切換到usr/local/nginx/conf/vhost/目錄下
[root@hanfeng ~]# cd /usr/local/nginx/conf/vhost/ [root@hanfeng vhost]# 新建新建一個虛擬主機(jī)test.com.conf,并編輯
[root@hanfeng vhost]# ls aaa.com.conf [root@hanfeng vhost]# vim test.com.conf 添加以下內(nèi)容 server { listen 80; server_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com; location / //表示全站,都需要進(jìn)行用戶認(rèn)證 #location /admin // 這個地方只要加上” /admin ” 就變成 針對這個站點(diǎn)的“admin” 這個目錄需要用戶認(rèn)證 #location ~ admin.php //如果把這行這樣寫,就會變成,匹配 “ admin.php ”這個頁面的時候才需要用戶認(rèn)證 { auth_basic "Auth"; //定義用戶認(rèn)證的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; //用戶名密碼文件 } } 保存退出 在配置完成后,需要生成密碼文件 在生成密碼文件,需要用到Apache生成密碼文件的工具“ htpasswd ” 若本機(jī)已經(jīng)安裝過Apache,可以直接使用命令htpasswd進(jìn)行生成
/usr/local/apache2.4/bin/htpasswd 若是本機(jī)未安裝Apache,可直接 yum install -y httpd 進(jìn)行安裝,因?yàn)閥um安裝的,所以工具存放在/usr/bin/下,可以直接使用htpasswd
yum install -y httpd 這里由于未安裝過Apache,所以先yum安裝
[root@hanfeng vhost]# yum install -y httpd 在yum安裝后,可以直接使用htpasswd命令 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]# 使用cat 命令查看/usr/local/nginx/conf/htpasswd 文件,會看到生成了一行字符串
[root@hanfeng vhost]# cat /usr/local/nginx/conf/htpasswd hanfeng:$apr1$Vvig1g73$oHYs5Ng/ubqoYXzZT4TWP/ [root@hanfeng vhost]# 關(guān)于htpasswd -c 命令 第一次創(chuàng)建的時候因?yàn)闆]有htpasswd這個文件,需要-c創(chuàng)建,第二使用的時候因?yàn)橐呀?jīng)有這個htpasswd文件了,將不再需要-c 選項,如果還繼續(xù)使用-c 這個選項,將會重置 htpasswd里的東西 再來htpasswd指定文件,生成另一個用戶
[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]# 檢查配置nginx文件是否存在語法錯誤
[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]# 重新加載配置文件 在重新加載的時候,若配置文件中存在錯誤,配置文件將不會生效; 如果是直接使用restart,如果配置有錯,將會直接影響到網(wǎng)站的運(yùn)行
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload [root@hanfeng vhost]# 測試
[root@hanfeng vhost]# curl -x127.0.0.1:80 test.com
401 Authorization Required
[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com
404 Not Found
[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]# 這里的用戶認(rèn)證是針對整站 針對某一個目錄下,才需要認(rèn)證 比如訪問admin的時候,才需要認(rèn)證 首先訪問admin嘗試下
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/
404 Not Found
[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]# 這時候再來訪問test.com,就不需要指定用戶名和密碼了
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com “test.com” [root@hf-01 vhost]#
6.訪問test.com/admin/目錄
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
401 Authorization Required
[root@hf-01 vhost]# mkdir /data/wwwroot/test.com/admin [root@hf-01 vhost]# 然后在admin目錄下新建index.html
[root@hf-01 vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/adm in/index.html [root@hf-01 vhost]# 這時再來訪問 test.com/admin/ 會顯示401,但是指定用戶名和密碼后就會正常顯示
[root@hf-01 vhost]# curl -x127.0.0.1:80 test.com/admin/
401 Authorization Required
[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]# 這時候就可以直接訪問 test.com/admin/,不需要指定用戶名和密碼了,但是在訪問admin.php的時候,則會顯示401——>狀態(tài)碼為401說明需要驗(yàn)證
[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
401 Authorization Required
文章題目:12.8Nginx用戶認(rèn)證
網(wǎng)址分享:http://www.dlmjj.cn/article/choesj.html