Nginx裡的location以及如何使用者認證

來源:互聯網
上載者:User

標籤:nginx   使用者認證   location   配置   

Nginx玩的就是“頁面輸出”,把新聞的頁面放到新聞的檔案夾裡,把體育的頁面放到體育的檔案夾裡,把動漫的頁面放到動漫的檔案夾裡,然後搭配好各種正則搜尋,這樣使用者在瀏覽器的地址欄裡輸入對應的地址,伺服器就回到相應的檔案夾裡去把網頁呈現出來。


location的定位

伺服器裡的nginx.conf配置

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M00/7F/BF/wKioL1crQguy_1juAAA8NJOsgQU939.png" title="1.png" alt="wKioL1crQguy_1juAAA8NJOsgQU939.png" />

這個網域名稱的server_name是localhost。location /裡的/指的是 /usr/local/nginx/html 這個目錄,此時,在/usr/local/nginx/html下建立一個叫welcome.html的檔案,裡面的內容是:“welcome to ChrisChan‘s server"。


然後#nginx -t,檢查一下設定檔,都OK之後,就#nginx -s reload。

使用瀏覽器開啟localhost可以看一下效果。

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/7F/BF/wKioL1crREHhcIMSAAAsxhhJ04g813.png" title="1.png" alt="wKioL1crREHhcIMSAAAsxhhJ04g813.png" />

但是後面我還設定了一個二階網頁,/nba,雖然它的root也是/usr/local/nginx/html,但是這個index.html卻不可以設定在/usr/local/nginx/html,而是要設定到/usr/local/nginx/html/nba裡。於是在/usr/local/nginx/html/nba這個檔案夾裡,#echo "steven curry is MVP in 2016!" > index.html,同樣重新檢查nginx的設定檔文法然後重啟,在瀏覽器看一下效果。

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/7F/BF/wKioL1crRWTTNVKhAAA0tSEb49s180.png" title="1.png" alt="wKioL1crRWTTNVKhAAA0tSEb49s180.png" />

這裡需要帳號密碼,在設定檔裡我們已經看到了,帳號密碼都被記錄在/usr/local/nginx/conf/passwd這個檔案夾裡,這樣防止陌生人來訪問這個網頁。這個密碼是可以被cookie記錄的,而且上面的提示不可以是中文。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/7F/BF/wKioL1crSPawLcNCAAAyzau-l_0175.png" title="1.png" alt="wKioL1crSPawLcNCAAAyzau-l_0175.png" />

【總結】可以看出在瀏覽器地址欄裡localhost/是指nginx.conf配置的root一大串地址,而localhost/nba裡的檔案對應是root/nba檔案夾裡的。


nginx如何使用者驗證

nginx使用者驗證有很多方法,http://dreamfire.blog.51cto.com/418026/1141385/這個文章就說明了一個方法,但是有一個方法比這個簡單的多,使用ngx_http_auth_basic_module模組。


在nginx.conf裡對應的網頁寫上

     

   location /                     #這裡可以是任何尾碼或者不加尾碼        {                auth_basic "ABC";   #“ABC就是使用者登入的提示                auth_basic_user_file /usr/local/nginx/conf/passwd;    #這裡是密碼檔案的路徑                 autoindex on        }

備忘:一定要注意auth_basic_user_file路徑,否則會不厭其煩的出現403。


然後把設定檔儲存退出,下面就是產生passwd這個密碼檔案:

# printf "chris:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd# cat conf/htpasswd chris:xyJkVhXGAZ8tM        <----這個就是帳號密碼

 然後#nginx -s reload,查看一下效果即可。如果使用者認證失敗,就會401 authorization required,這個密碼是會被cookie記錄的。




本文出自 “生活就是等待戈多” 部落格,請務必保留此出處http://chenx1242.blog.51cto.com/10430133/1770572

Nginx裡的location以及如何使用者認證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.