日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
12.8Nginx用戶認(rèn)證

Nginx用戶認(rèn)證目錄概要 vim /usr/local/nginx/conf/vhost/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 / { 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)證

昂仁網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(liá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

401 Authorization Required


nginx/1.12.1
[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]# 會提示錯誤碼401,就是需要用戶,所以用curl指定用戶 這時指定用戶和密碼再來訪問,會提示404,這是因?yàn)槿ピL問index.html,但是還未創(chuàng)建

[root@hanfeng vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com 404 Not Found

404 Not Found


nginx/1.12.1
[root@hanfeng vhost]# 創(chuàng)建目錄,然后新建index.html

[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

404 Not Found


nginx/1.12.1
[root@hf-01 vhost]# 然后在/usr/local/nginx/conf/vhost/test.com.conf配置文件中定義,只需要在location / 后加上admin/ 目錄即可

[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

401 Authorization Required


nginx/1.12.1
[root@hf-01 vhost]# 這時創(chuàng)建一個測試頁面 先新建目錄

[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

401 Authorization Required


nginx/1.12.1
[root@hf-01 vhost]# curl -uhanfeng:hanfeng -x127.0.0.1:80 test.com/admin/ test.com admin dir [root@hf-01 vhost]# 針對URL 比如針對admin.php 首先在配置文件/usr/local/nginx/conf/vhost/test.com.conf下定義,在 location 后加~ admin.php即可

[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

401 Authorization Required


nginx/1.12.1
[root@hf-01 vhost]#


文章題目:12.8Nginx用戶認(rèn)證
網(wǎng)址分享:http://www.dlmjj.cn/article/choesj.html