宝塔面板配置SSL,实现tomcat静态资源使用https

在使用tomcat+nginx时,如果是nginx代理https请求,可能会出现页面可以访问,但是无法加载js和css,这是由于nginx代理https,而nginx和tomcat之间是http,会报:The page at ‘https://XXX’ was loaded over HTTPS, but requested an insecure…

让tomcat知道nginx发来的是http还是https。默认情况下,nginx得到的https的访问会以http的方式发给tomcat。

1、配置nginx

 location /
    {
        proxy_pass "http://app.sanyue100.com:8081";
        proxy\_set\_header Host app.sanyue100.com;
        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;
    }
    
    location ~ .*\\\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
    {
        proxy_pass http://xxx.xxxx.com:8081;
        expires      12h;
    }

2、配置tomcat的server.xml

 Engine 模块下配置一个 Value:
<Valve className=“org.apache.catalina.valves.RemoteIpValve”
remoteIpHeader=“X-Forwarded-For” protocolHeader=“X-Forwarded-Proto”
protocolHeaderHttpsValue=“https”/>

配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。

X-Forwarded-For 是为了获得实际用户的 IP。