全球主机交流论坛

标题: 如何设置 nginx 目录密码保护 [打印本页]

作者: tumour    时间: 2009-9-9 13:11
标题: 如何设置 nginx 目录密码保护
需求,web站点某目录下test 进入时需要输入密码才可以进入
类似apache的目录保护,在apache下很容易可以实现,但是不知道在nginx下如何设置

[ 本帖最后由 tumour 于 2009-9-9 16:37 编辑 ]
作者: cpuer    时间: 2009-9-9 13:20
标题: 回复 1# 的帖子
好像要使用
auth_basic “Restricted”;
auth_basic_user_file htpasswd;
,具体怎么设置没搞过。
作者: tumour    时间: 2009-9-9 13:48
没人有这样的需求呀?
作者: cpuer    时间: 2009-9-9 13:55
标题: 回复 3# 的帖子
我还没有这样的需求,做什么网站有这样的需求?
作者: tumour    时间: 2009-9-9 14:02
单位论坛,不对外开放。
作者: cpuer    时间: 2009-9-9 14:04
原帖由 tumour 于 2009-9-9 14:02 发表
单位论坛,不对外开放。


那用内部IP或者论坛只允许单位IP访问,或者只有帐号和密码才能访问。
作者: tumour    时间: 2009-9-9 14:06
单位非官方论坛,私下搞的
作者: cpuer    时间: 2009-9-9 14:20
原帖由 tumour 于 2009-9-9 14:06 发表
单位非官方论坛,私下搞的


呵呵,那就等高手回复
作者: zyypp    时间: 2009-9-9 14:29
中文版
http://wiki.nginx.org/NginxChsHttpAuthBasicModule

英文版
http://wiki.nginx.org/NginxHttpAuthBasicModule

我去找地方 试试去 嘿嘿
作者: cpuer    时间: 2009-9-9 14:43
标题: 回复 9# 的帖子
期待结果和中文详细教程
作者: tumour    时间: 2009-9-9 14:59
不错,期待中……
作者: zyypp    时间: 2009-9-9 15:15
比如要对 网站目录下的 test 文件夹 进行加密认证

那么 在 nginx.conf 文件中对应的 server 段中 添加
location ^~ /test/ {
auth_basic            "TEST-Login";
auth_basic_user_file  /root/htpasswd;
}

再在 root 的主目录中 /root/ 创建一个新文件 htpasswd
此文件的书写格式是
用户名:密码
每行一个账户
并且 密码必须使用函数 crypt(3) 加密

官方档说 可以用 Apache 的 htpasswd 工具来创建密码文件
当然也可以使用perl 创建密码文件 新建 一个 pw.pl 文件 其内容:
#!/usr/bin/perl
use strict;

my $pw=$ARGV[0] ;
print crypt($pw,$pw)."\n";

然后执行
chmod +x pw.pl
./pw.pl password
papAq5PwY/QQM

papAq5PwY/QQM 就是password 的crypt()密码
然后 将上面用 perl 生成的 加密后的密码
按照
用户名:密码
的格式写到 htpasswd 文件中

这样既完成了设置

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

还有 htpasswd 这个文件的名字可以 自己随意设置
用户名 也是 自己随意设置 无需加密
密码必须使用函数 crypt(3) 加密
作者: zyypp    时间: 2009-9-9 15:16
写的 有点乱 啊 见谅 呵呵 C大 要是有心情 可以帮着 编辑下 嘿嘿
作者: cpuer    时间: 2009-9-9 15:18
标题: 回复 13# 的帖子
不觉得乱,有条理。 kaning
作者: cpuer    时间: 2009-9-9 15:19
如果是子目录的子目录上限制的话怎么写?
作者: zyypp    时间: 2009-9-9 15:23
标题: 回复 15# 的帖子
子目录的子目录 比如是 123
那么 直接写
^~ /test/123/
或者
^~ /123/
作者: cpuer    时间: 2009-9-9 15:40
标题: 回复 16# 的帖子

作者: tumour    时间: 2009-9-9 15:43
密码必须使用函数 crypt(3) 加密
意思就是说,明文下看到的不是真实密码啦?
作者: tumour    时间: 2009-9-9 15:45
可以单独安装Apache 的 htpasswd 工具嘛?
如果我卸掉apache,是不是htpaswd工具也没掉了。
作者: zyypp    时间: 2009-9-9 15:49
标题: 回复 19# 的帖子
不是非要 用 Apache 的 htpasswd
可以用上面的 perl 脚本
呵呵

最终 明文显示的 密码 是经过加密后的
作者: zyypp    时间: 2009-9-9 15:51
标题: 回复 17# 的帖子
C大 每次都是 只给 30分 下会 给多点吧 比如 来个几十W 嘿嘿 O(∩_∩)O哈哈~
作者: tumour    时间: 2009-9-9 15:56
zyypp
来看看
http://443502.xyz/viewthrea ... p;extra=&page=5
作者: zyypp    时间: 2009-9-9 16:00
标题: 回复 22# 的帖子
已经回复了 嘿嘿
作者: cpuer    时间: 2009-9-9 16:11
原帖由 zyypp 于 2009-9-9 16:00 发表
已经回复了 嘿嘿


http://443502.xyz/viewthrea ... amp;extra=#pid33953
作者: zyypp    时间: 2009-9-9 17:15
标题: 回复 24# 的帖子
。。。。
作者: cpuer    时间: 2009-9-9 17:16
标题: 回复 25# 的帖子

作者: tumour    时间: 2009-9-11 17:33
标题: 回复 12# 的帖子
测试发现,是可以弹出窗口要求输入用户名和密码
但是进入后提示500错误。
作者: cpuer    时间: 2009-9-11 18:00
标题: 回复 27# 的帖子

作者: tumour    时间: 2009-9-11 18:01
想用nginx 就得抱有折腾的准备……
作者: cpuer    时间: 2009-9-11 18:29
标题: 回复 29# 的帖子
折腾到疯狂。
作者: zyypp    时间: 2009-9-11 18:40
标题: 回复 27# 的帖子
....我试验的就 ok 啊 好奇
作者: cpuer    时间: 2009-9-11 18:43
原帖由 zyypp 于 2009-9-11 18:40 发表
....我试验的就 ok 啊 好奇


帖出配置表。
作者: tumour    时间: 2009-9-11 18:55
[root@centos conf]# cat nginx.conf
user  www www;

worker_processes 1;

error_log  /web/logs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
        {
                use epoll;
                worker_connections 51200;
        }

http
        {
                include       mime.types;
                default_type  application/octet-stream;

                #charse  gb2312;

                server_names_hash_bucket_size 128;
                client_header_buffer_size 32k;
                large_client_header_buffers 4 32k;
                client_max_body_size 8m;

                sendfile on;
                tcp_nopush     on;

                keepalive_timeout 60;

                tcp_nodelay on;

                fastcgi_connect_timeout 300;
                fastcgi_send_timeout 300;
                fastcgi_read_timeout 300;
                fastcgi_buffer_size 64k;
                fastcgi_buffers 4 64k;
                fastcgi_busy_buffers_size 128k;
                fastcgi_temp_file_write_size 128k;

                gzip on;
                gzip_min_length  1k;
                gzip_buffers     4 16k;
                gzip_http_version 1.0;
                gzip_comp_level 2;
                gzip_types       text/plain application/x-javascript text/css application/xml;
                gzip_vary on;

                #limit_zone  crawler  $binary_remote_addr  10m;

server
        {
                listen       80;
                server_name test.net;
                index index.html index.htm index.php;
                root  /web/www;

                #limit_conn   crawler  20;

                #location /status {
                #stub_status    on;
                #access_log     off;
                #}

                location ~ .*\.(php|php5)?$
                        {
                                fastcgi_pass  unix:/tmp/php-cgi.sock;
                                #fastcgi_pass  127.0.0.1:9000;
                                fastcgi_index index.php;
                                include fcgi.conf;
                        }

                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                        {
                                expires      30d;
                        }

                location ~ .*\.(js|css)?$
                        {
                                expires      12h;
                        }
                location ^~/test/{
                auth_basic      "TEST-Login";
                auth_basic_user_file /root/htpasswd;
                }
                log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
                access_log  /web/logs/access.log  access;
        }

server
        {
                listen  80;
                server_name  status.lnmp.org;

                location / {
                        stub_status on;
                        access_log   off;
                }
        }
}

-----------------------------------------------------
密码文件已经建立,test文件夹下放置的是个 index.html 文件
作者: tumour    时间: 2009-9-11 19:23
自己已经创建 pw.pl 文件
用它来创建加密密码。

[ 本帖最后由 tumour 于 2009-9-11 19:33 编辑 ]
作者: zyypp    时间: 2009-9-11 19:48
nginx.conf
user  www www;
worker_processes 1;
error_log  /logs/nginx_error.log  crit;
pid        /nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
  use epoll;
  worker_connections 51200;
}
http
{
  include       mime.types;
  default_type  application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 64k;
  client_max_body_size 8m;
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
upstream ipmac {
  server 192.168.0.254:81 weight=1;
  }
server
{
   listen       80;
   server_name    192.168.0.205
   index index.html index.htm index.php;
proxy_temp_path /htdocs/tmp;
   root  /htdocs/www;
location / {
autoindex on;
}
location ^~ /pass/123/ {
auth_basic            "Restricted";
auth_basic_user_file  /nginx/conf/htpasswd;
}

location ^~ /ip {
  proxy_pass http://192.168.0.254:81/;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout      300;
proxy_read_timeout      300;
proxy_buffer_size      64k;
proxy_buffers          4 62k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}

   location ~ .*\.(php|php5)?$
   {
     fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_index index.php;
     include fcgi.conf;
   }

location /status {
   access_log   off;
   }

   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
   access_log  /logs/access.log  access;
   }
}

htpasswd
haha:qqqDZ5y6EacRg

账户名 haha
密码  qqaazaq
作者: cpuer    时间: 2009-9-11 19:49
location ^~/test/{
                auth_basic      "TEST-Login";
                auth_basic_user_file /root/htpasswd;
                }

看起来没问题的说。
作者: zyypp    时间: 2009-9-11 20:10
标题: 回复 36# 的帖子
对啊
作者: klgd    时间: 2010-6-3 22:10
不错的教程,我收藏了
作者: amwlsai    时间: 2010-7-3 16:06
标题: 嗯,这个很 好, 收藏了!
嗯,这个很 好, 收藏了!




欢迎光临 全球主机交流论坛 (https://443502.xyz/) Powered by Discuz! X3.4