我已经在托管jenkins和其他一些应用程序的ubuntu实例上设置了nginx作为反向代理。我正在使用nginx根据相对路径路由到各种应用程序。从客户端到nginx的所有流量都超过了https。在防火墙后面,nginx通过http将所有内容路由到配置的路径和端口号。它看起来像这样:
firewall
|
|
--->https--->nginx---http--->jenkins
|
|
nginx配置文件的相关部分是:
server {
listen 443 ssl;
ssl_certificate cert.crt;
ssl_certificate_key cert.key;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location /jenkins {
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:6969;
}
}
问题是jenkins使用简单的身份验证,并且在成功登录后,它会发送302重定向。 Nginx正确代理URL和端口,但不是方案。因此客户端通过http而不是https跟踪重定向。在浏览器中,我得到400错误:
400 Bad Request The plain HTTP request was sent to HTTPS port
我知道有一个方案变量:$ scheme。但我不知道如何告诉nginx将jenkins的http重定向映射到https。我在stackoverflow上看到的所有examples似乎都解决了稍微不同的情况。
答案 0 :(得分:2)
答案 1 :(得分:2)
好吧,我遇到了同样的问题,经过更多研究和几次反复尝试后,我发现了这个问题。
尝试添加标题X-Forwarded-Proto,如以下示例所示,
server {
server_name example.com;
proxy_set_header Host $host;
# You need this line
proxy_set_header X-Forwarded-Proto $scheme;
location ^~ /jenkins {
proxy_pass http://localhost:8080/;
}
listen 443 ssl;
ssl_certificate cert.crt;
ssl_certificate_key cert.key;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
}
答案 2 :(得分:0)
我无法使用proxy-redirect,rewrite和return指令来实现我正在寻找的行为。设置jenkins以使用https解决了这个问题。