nginx反向代理配置https和http能同時訪問后,發(fā)現(xiàn)一些頁面使用http訪問正常,使用https訪問頁面的js和css等靜態(tài)頁面加載不出來
報錯:This request has been blocked; the content must be served over HTTPS,如下圖:

問題原因:HTTPS頁面里動態(tài)的引入了HTTP資源,比如引入一個js文件,會被直接block掉的.在HTTPS頁面里通過AJAX的方式請求HTTP資源,也會被直接block掉。
解決方法一:(沒試過,要改的頁面太多了,沒有使用該方法)
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
可以在相應(yīng)的頁面的<head>里加上這句代碼,意思是自動將http的不安全請求升級為https
解決方法二:修改 nginx 的 proxy 配置部分
add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; add_header Content-Security-Policy upgrade-insecure-requests; #自動升級請求,加載 http 資源時自動替換成 https 請求 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

/opt/nginx/sbin/nginx -s reload 即可
好記性不如爛筆頭,最難不過堅持

浙公網(wǎng)安備 33010602011771號