發表文章

目前顯示的是有「basic auth」標籤的文章

[Nginx] 幫 nginx 特定網址加上 basic auth

Ref: http://www.ttlsa.com/nginx/nginx-basic-http-authentication/ https://serverfault.com/questions/505482/nginx-auth-only-for-given-location 假如說有需要對特定的網址加上簡單的驗證保護 又懶得去新增比較複雜的會員機制 就可以考慮簡單的 Nginx Basic Auth 這個方法 一般情況下, ngx_http_auth_basic_module 是已經被包含在預設安裝內了 所以假設我需要對 /abc 這個網址去做驗證 可以新增以下語法(以PHP為例) location = /abc { auth_basic "basic http auth for this site"; auth_basic_user_file /etc/nginx/conf.d/htpasswd; try_files $uri $uri/ /index.php?$args; } 然後可以使用 htpasswd 來驗證 以上面的例子而言, 可以將帳號密碼新增到 /etc/nginx/conf.d/htpasswd內 假設新增帳號: abc, 密碼: 123456 printf "abc:$(openssl passwd -crypt 123456)\n" >>/etc/nginx/conf.d/htpasswd 接著可以確認自己 nginx 有沒有出錯 可以使用以下指令並 reload nginx nginx -t nginx -s reload