nginx 反向代理重定向到 http 而不是 https

nginx reverse proxy redirects to http instead of https

具有以下设置:NGINX(端口 443)> Jetty(端口 9090)> Spring 控制器

为了简化问题,我使用了以下文件:

使用 HTTP 没有问题,但是在将 NGINX 配置切换到 HTTPS 后,我在浏览器中收到以下错误并且没有显示 iframe:

main.html:7 Mixed Content: The page at 'https://dev/main.html' was loaded over HTTPS, but requested an insecure frame 'http://dev/iframe.html'. This request has been blocked; the content must be served over HTTPS.

所以控制器重定向到 http 而不是 https,这是我的 NGINX 配置,根据我的理解应该让 jetty/controller 知道它是 运行 on https:

 server {
    listen 443      ssl http2;
    listen [::]:443 ssl http2;
    server_name dev;

    ssl on;
    ssl_certificate ...;
    ssl_certificate_key ...;

    location / {
        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_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Port $server_port;
        
        proxy_ssl_name $host;
        proxy_ssl_server_name on;

        proxy_pass http://127.0.0.1:9090;
    }
}

你需要做一些事情...

  1. 在 nginx 方面,使用来自 RFC7239 的标准 Forwarded header,而不是 X-Forwarded-* header。这是因为 X-Forwarded-* header 和 不是标准 并且在它们的用法中存在冲突。 (在您的示例中,您将端口分开,现在也与端口的“host”、“proto”和“for”用法冲突)

  2. 在 Jetty 端,启用 ForwardedRequestCustomizer。这将查找各种转发 headers 并适当更新请求的权限、原型和“安全”标志。

  3. 在 Jetty 端,将 HttpConfiguration.securePort 配置为 nginx 上 SSL/TLS 的端口,而不是 Jetty 本身使用的端口。