Nginx Web Server – 使用帳號及密碼來保護目錄

在 Nginx Web Server 要利用帳號及密碼來保護目錄的安全
安裝 apache2-utils 套件
# apt-get install apache2-utils

建立帳號及密碼檔
# /usr/bin/htpasswd -c /usr/share/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin

檢視密碼檔 admin/12345
# cat /usr/share/nginx/.htpasswd
admin:$apr1$jQjpLKJr$HgsSAhdBqfdN9l4IWkdjs.
[@more@]建立測試目錄
# mkdir /usr/share/nginx/www/security

建立測試檔案
# cat /usr/share/nginx/www/security/index.html
<h1>辛苦了,什麼東西都沒有!</h1>

修改 /etc/nginx/nginx.conf 設定檔
# vim /etc/nginx/nginx.conf
location /security {
                root /usr/share/nginx/www;
                index index.php index.html index.htm;
                auth_basic “Restricted”;
                auth_basic_user_file /usr/share/nginx/.htpasswd;
                location ~ ^/security/(.+.php)$ {
                     try_files $uri =404;
                     fastcgi_pass unix:/var/run/php5-fpm.sock;
                     fastcgi_index index.php;
                     fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/$fastcgi_script_name;
                     include fastcgi_params;
                   }
        }

重新啟動 Nginx Web Server
# /etc/init.d/nginx restart
Restarting nginx: nginx.

瀏覽設定的目錄
需要輸入帳號及密碼

輸入錯誤

輸入正確