1. 程式人生 > >nginx 用戶認證

nginx 用戶認證

nginx 用戶認證

作用:web上的一些內容不想被其他人知道,但是又想讓部分人看到。nginx的http auth模塊以及Apache http auth都是很好的解決方案。

默認情況下nginx已經安裝了ngx_http_auth_basic_module模塊,如果不需要這個模塊,可以加上 --without-http_auth_basic_module 。

配置:

修改nginx.conf 文件

server {

  1. listen 80;

  2. server_name localhost;

  3. auth_basic "Input Password:"; //認證提示符

  4. auth_basic_user_file "/usr/local/nginx/pass"

    ; //認證密碼文件

  5. location / {

  6. root html;

  7. index index.html index.htm;

  8. }

  9. }

生成密碼文件,創建用戶及密碼

使用htpasswd命令創建賬戶文件,需要確保系統中已經安裝了httpd-tools。

  1. [root@svr5 ~]# yum -y install httpd-tools

  2. [root@svr5 ~]# htpasswd -cm /usr/local/nginx/pass tom //創建密碼文件,註意pass的位置

  3. New password:

  4. Re-type new password:

  5. Adding password for user tom

  6. [root@svr5 ~]# htpasswd -m /usr/local/nginx/pass jerry

  7. //追加用戶,不使用-c選項

  8. New password:

  9. Re-type new password:

  10. Adding password for user jerry

    重啟Nginx服務


nginx 用戶認證