簡體   English   中英

如何處理nginx反向代理https到http方案重定向

[英]how to handle nginx reverse proxy https to http scheme redirect

我已經在托管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上看到的所有示例似乎都解決了略有不同的情況。

您可能需要從上游重寫302重定向。 假設其他一切都正確,請嘗試:

proxy_redirect http:// https://;

請參閱此文檔了解詳細信息

好吧,我遇到了同樣的問題,經過一些更多的研究和幾次嘗試和錯誤的嘗試,我發現了它。

嘗試添加標題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;

}

我無法使用proxy-redirect,rewrite和return指令來實現我正在尋找的行為。 設置jenkins以使用https解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM