在 Windows 下使用 Nginx 作为反向代理服务器

在 Windows 下使用 Nginx 作为反向代理服务器

增加一个包含文件

http {
include proxy.conf;
}

proxy.conf的内容如下:

server {
listen      8001;
server_name zhaidangwei;    # standard stuff
access_log  logs/hg-access.log;
error_log   logs/hg-error.log;
location / {
# limit_except GET # if we want to do this for all requests but GETS (public clone, private push)
auth_basic   "Jiucai HG Server";
auth_basic_user_file user.txt;
proxy_pass http://localhost:8000; # or what ever port your server is running.
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
proxy_connect_timeout 30;
proxy_read_timeout 120;

}
}

访问需要HTTP BASIC 认证,密码文件是 user.txt 在conf目录,
看看官方文档是如何说的:

官方俄文: http://sysoev.ru/nginx/docs/http/ngx_http_auth_basic_module.html
nginx社区英文:http://wiki.nginx.org/HttpAuthBasicModule
howtocn中文翻译:http://www.howtocn.org/nginx:nginx%E6%A8%A1%E5%9D%97%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E4%B8%AD%E6%96%87%E7%89%88:standardhttpmodules:httpauthbasic

auth_basic_user_file

语法:auth_basic_user_file the_file
默认值:no
使用字段:http, server, location, limit_except
指令为验证域指定了密码文件,0.6.7版本以后这里指定的文件是nginx.conf所在目录的相对路径,而不是–codefix指定的路径。
这个文件格式如下:

user:pass
user2:pass2:comment
user3:pass3

如果apache没有被安装,密码字段必须经过 crypt(3)函数加密,如果安装了apache,你可以使用Apache的htpasswd程序生成密码,
注意:apache使用MD5加密。

我配置了大半天都失败:
1、使用Windows下的Apache 的 htpasswd.exe 生成密码文件
2、使用别人在linux下 htpasswd 生成的文件
3、使用 crypt 函数生成的密码文件

都不行,最后,最简单的方法竟然可以,用户名和密码都是明文保存:

test:test

天哪、、、官方文档没试过吗?