我有这个奇怪的错误:
我有一个运行在8090的Jenkins服务,我想通过Nginx的auth_basic和proxy_pass进行密码保护。在没有auth_basic的情况下执行proxy-pass时,我可以访问这个站点,没有问题:
server { listen 8080; location / { proxy_pass http://localhost:8090; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_buffering off; proxy_pass_header Authorization; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # auth_basic "Restricted"; #For Basic Auth # auth_basic_user_file /etc/nginx/htpasswd; #For Basic Auth } }
但是,取消注释这两个身份validation行。 我在浏览器中获得预期的login页面。 如果我input无效的凭据,我在nginx / error.log中得到预期的错误:
(错误的用户)
2014/07/30 18:59:58 [error] 15374#0:*在“/ etc / nginx / htpasswd”,客户端:xx.xx.xx.xx,服务器:示例中未find用户“afds”。请求:“GET / job / xyz HTTP / 1.1”,主机:“example.com:8080”
(好用户,密码错误)
2014/07/30 19:01:43 [error] 15374#0:* 44用户“jenkins”:密码不匹配,客户端:xx.xx.xx.xx,服务器:example.com,请求:“GET / job / xyz HTTP / 1.1“,主机:”example.com:8080“
但是,如果我input正确的用户/密码组合,我没有得到任何东西在nginx / error.log,而是我在nginx / access.log
xx.xx.xx.xx – jenkins [30 / Jul / 2014:19:03:53 -0500]“GET / job / xyz HTTP / 1.1”401 278“ – ”“Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_4)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 36.0.1985.125 Safari / 537.36“
浏览器再次请求密码。
任何想法为什么发生这种情况?
正如@Lukas所解释的那样,将授权标头转发到后端将使您的客户端尝试对其进行身份validation。 由于您可能尚未在您的后端定义任何身份validation,因此将按照RFC 2617的要求使用401进行回答:
如果源服务器不希望接受与请求一起发送的证书,它应该返回401(未授权)响应。
你的后端与authentication无关,因为它是由代理完成的。 不要代理该标题字段。
如果你希望你的后端再次validation客户端,你也应该使用相同的用户/密码数据库来激活auth_basic 。 pipe理用户时要小心,您将有2个副本保持同步现在…