nginx proxy_pass重写响应头的位置

这个nginx实例的目的是让GitLab和OpenWRT Luci通过一个反向代理redirect。 它已经为其他几个网站工作,所有这些网站都有一个基本的url,似乎对付这个问题。

  • 本例中的GitLab在端口9000上的本地服务器上。
  • nginx网站在8080端口上。
  • OpenWRT具有完全相同的问题,但使用/ cgi-bin / luci /

示例位置的相关nginxconfiguration是;

location /gitlab/ { proxy_pass http://127.0.0.1:9000/; proxy_redirect default; } 
  • 请注意,结果是相同的和没有一个结尾的斜线。

有一些头代理configuration选项被应用到这个位置。

 # Timeout if the real server is dead proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; # Basic Proxy Config proxy_set_header Host $host:$server_port; proxy_set_header Origin $scheme://$host:$server_port; proxy_set_header Connection $http_connection; proxy_set_header Cookie $http_cookie; proxy_set_header Upgrade $http_upgrade; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Scheme $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; proxy_set_header X-Frame-Options SAMEORIGIN; # Advanced Proxy Config send_timeout 5m; proxy_read_timeout 300; proxy_send_timeout 300; proxy_connect_timeout 300; proxy_buffers 32 4k; proxy_buffer_size 4k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_http_version 1.1; proxy_cache_bypass $cookie_session; proxy_no_cache $cookie_session;] 
  • 将#proxy_set_header主机注释掉,而是将浏览器redirect到https://127.0.0.1:9000/users/sign_in

浏览到https://website.com:8080/gitlab/ ;

 GET /gitlab/ HTTP/1.1 Host: website.com:8080 

响应不正确地返回到/users/sign_in而不是/gitlab/users/sign_in

 HTTP/1.1 302 Found Cache-Control: no-cache Connection: keep-alive Content-Type: text/html; charset=utf-8 Location: https://website.com:8080/users/sign_in 

手动浏览到https://网站:8080 / gitlab / users / sign_in加载页面,但没有资产,因为它们落在上面的相同问题。

GitLab资产失败

阅读nginx文档 ,它表明,默认的代理行为应该处理这种情况,虽然它似乎失败了。

日志似乎并不多。

应采取哪些额外步骤来帮助诊断这可能发生的原因?

向您的proxy_pass目标添加一个尾部的斜杠。

更新: OP没有精确的虚拟主机接受https 。 由于该scheme通过额外头文件被转发到后端服务器,因此自proxy_redirect default;以来就会发生问题proxy_redirect default; 在重写上游回复中的Location标题时,命令nginx 默认使用httpscheme ,而不是https。

所以,这必须明确地更改为一个更通用的forms(尾部斜线仍然是必要的):

 location /gitlab/ { proxy_pass http://127.0.0.1:9000/; proxy_redirect $scheme://$host:$server_port/ /gitlab/; }