1. 程式人生 > >解決Nginx+Tomcat中https轉http請求問題

解決Nginx+Tomcat中https轉http請求問題

今天遇到一個問題,在配置Nginx的https請求,tomcat使用http時,請求靜態資源會報

Mixed Content: The page at 'https://a.b.com/detail?id=5' was loaded over HTTPS, but requested an insecure script 'http://a.b.com/xxxxxxx'. This request has been blocked; the content must be served over HTTPS.的問題。

解決方案:
1 Nginx對應server的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;

注意:如果nginx裡配置的upstream是非具體IP的,不要寫localhost,應該寫127.0.0.1,類似如下:

upstream xxx{
        server 127.0.0.1:8080 weight=1;
    }

2 Tomcat的配置srever.xml
2.1 connector裡新增

redirectPort="443" proxyPrort="443"

2.2 Host裡新增

<Valve className="org.apache.catalina.valves.RemoteIpValve" protocolHeaderHttpsValue="https" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" />

3 在防火牆裡新增443埠的入站規則