使用nginx反向代理时,需要留意auth_basic是否也转给了后端
情况:nginx监听80端口,反向代理后边的tomcat,且nginx配置了auth_basic,凭密码访问。输入正确的用户名和密码,但nginx总是提示错误。
过程:
1.查看nginx错误日志,有提示用户不存在的内容,如 user "liujia7" was not found in "D:\nginx-1.15.6/conf/passwd" ,但我明明输入的是用户名是 liujia
2.暂时移除nginx配置文件里的proxy_pass,再次测试,能正常验证密码
3.搜索网上资料,在nginx文件里恢复proxy_pass同时加上proxy_set_header Authorization "";
即验证信息不转给后端的tomcat,最后测试,也能正常验证密码
location / {
root html;
auth_basic "Restricted";
auth_basic_user_file passwd;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
proxy_pass http://tomcat_server$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#index index.html index.htm;
proxy_set_header Authorization "";
}