Chinaunix首页 | 论坛 | 博客
  • 博客访问: 627381
  • 博文数量: 197
  • 博客积分: 4858
  • 博客等级: 上校
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-06 22:46
文章分类

全部博文(197)

文章存档

2011年(30)

2010年(21)

2009年(25)

2008年(80)

2007年(41)

分类: BSD

2011-11-11 20:07:42

 
 
 

整理下nginx下为目录增加用户认证的配置:

nginx的auth_basic认证采用与apache兼容的密码文件,因此我们需要通过apache的htpasswd生成密码文件。

找到htpasswd文件地址。

找到htpasswd文件后,我们来创建一个用户,比如这个用户叫:test

 

1
/usr/bin/htpasswd –c /usr/local/ngnix/conf/authdb test

 

上面的命令在nginx的配置文件目录创建了用户为test的authdb密码文件,当然你也可以创建的在其他地方,此处nginx配置文件使用比较方便。

接着修改nginx的配置文件,在某个需要加auth_basic的server配置下添加如下内容


location /admin/ {     
auth_basic "Test Auth!";     
auth_basic_user_file /usr/local/ngnix/conf/authdb;
}


最后让nginx使用最新的配置:


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


补充一下,如果你使用了集群环境,那么还需要加Proxy_Pass:

location /admin/ {    
proxy_pass
auth_basic "Test Auth!";     
auth_basic_user_file /usr/local/ngnix/conf/authdb;
}

如果想限制某一个目录的话需要如下配置:

location ^~ /test/ {
auth_basic "TEST-Login!";
auth_basic_user_file /opt/nginxpwd;
}

如果 不用 ^~ /test/ 而用 /test 的话 那么将只能对目录进行验证直接访问其下的文件,将不会弹出登录验证

最后nginx.conf文件如下:

XML/HTML代码
  1. user www www;   
  2. worker_processes  1;   
  3. pid        /var/run/nginx.pid;   
  4. events {   
  5.     worker_connections  200;   
  6. }   
  7. http {   
  8.     include       mime.types;   
  9.     default_type  application/octet-stream;   
  10.     sendfile        on;   
  11.     keepalive_timeout  65;   
  12.     server {   
  13.         listen       8080;   
  14.         server_name  localhost;   
  15.         charset gb2312;   
  16.         location / {   
  17.             fancyindex on;   
  18.             fancyindex_exact_size off;   
  19.             root   /data/file;   
  20.             index  index.html index.htm;   
  21.         }   
  22.         error_page   500 502 503 504  /50x.html;   
  23.         location = /50x.html {   
  24.             root   /data/nginx-dist;   
  25.         }   
  26.         location /NginxStatus {   
  27.             stub_status on;   
  28.             access_log on;   
  29.             auth_basic "NginxStatus";   
  30.             auth_basic_user_file /data/passwds;   
  31.         }   
  32.         location ^~ /test1/ {   
  33.             auth_basic "Please input your Passwords!";   
  34.             auth_basic_user_file /data/passwds;   
  35.             fancyindex on;   
  36.             fancyindex_exact_size off;   
  37.             root   /data/file;   
  38.             index  index.html index.htm;   
  39.         }   
  40.   
  41.         location ^~ /test/ {   
  42.             auth_basic "Please input your Passwords!";   
  43.             auth_basic_user_file /data/passwds;   
  44.             fancyindex on;   
  45.             fancyindex_exact_size off;   
  46.             root   /data/file;   
  47.             index  index.html index.htm;   
  48.         }   
  49.     }   
  50. }   

Tags: ,

阅读(2254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~