问题:
网站一直用http,最近增加了https,要求http与https同时正常访问。
http页面引用https资源(如css/js等)可正常访问,但反之则报错,提示
Mixed Content: The page at 'https://…’ was loaded over HTTPS, but requested an insecure stylesheet 'http://…’. This request has been blocked; the content must be served over HTTPS.
思路:
把页面中的资源引用改为https
解决:
1、直接把原http页面中的资源引用改成https,这是最直接的办法,如果需要修改的页面较少同时页面中资源引用也较少,是可行的
2、在页面header中增加标签,使页面中的资源引用强制升级成https,只需每个页面增加一行代码,如果页面较少,也可行
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />
3、如果页面特别多,可以在服务器端返回响应头信息中添加以上指令,以nginx为例,在https访问网站的server配置中添加以下指令
add_header Content-Security-Policy upgrade-insecure-requests;
4、对于阿里云CDN加速的资源,需要在阿里云CDN控制台手动增加以上指令。具体位置是:CDN > 域名管理 > 回源配置 > 回源HTTP响应头,key是Content-Security-Policy,value是upgrade-insecure-requests
以上