Nginx 的 .htpasswd 設定


網站的設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
charset utf-8;
server_name protected.example.com;
root /path/to/protected.example.com;
index index.html index.htm index.php;

auth_basic "Authorized users only";
auth_basic_user_file /path/to/protected.example.com/.htpasswd;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

生成對應的 .htpasswd 檔案

1
echo USERNAME:`php -r 'echo crypt('PASSWORD', base64_encode('PASSWORD'));'` >> /path/to/protected.example.com/.htpasswd;

修改生成出來的檔案權限

1
chmod USERNAME:USERGROUP /path/to/protected.example.com/.htpasswd

重新讀取設定

1
/usr/local/nginx/sbin/nginx -s reload