nginx 用戶認證
作用:web上的一些內容不想被其他人知道,但是又想讓部分人看到。nginx的http auth模塊以及Apache http auth都是很好的解決方案。
默認情況下nginx已經安裝了ngx_http_auth_basic_module模塊,如果不需要這個模塊,可以加上 --without-http_auth_basic_module 。
配置:
修改nginx.conf 文件
server {
listen 80;
server_name localhost;
auth_basic "Input Password:"; //認證提示符
auth_basic_user_file "/usr/local/nginx/pass"
location / {
root html;
index index.html index.htm;
}
}
生成密碼文件,創建用戶及密碼
使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。
[root@svr5 ~]# yum -y install httpd-tools
[root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass tom //創建密碼文件,註意pass的位置
New password:
Re-type new password:
Adding password for user tom
[root@svr5 ~]# htpasswd -m /usr/local/nginx/pass jerry
//追加用戶,不使用-c選項
New password:
Re-type new password:
Adding password for user jerry
重啟Nginx服務
nginx 用戶認證