让页面中的http请求转为https请求

在 HTTPS 承载的页面上不允许出现 http 请求,一旦出现就是提示或报错让页面中的http请求转为https请求 第2张插图

因此即使使用到的域名对http请求已经强制了https,但是在页面中浏览器对于http请求压根就不会发送,那么就必需替换页面上所有的http为https

于是从http协议入手,在响应header中添加upgrade-insecure-requests,即在php入口文件中添加:

```header("Content-Security-Policy: upgrade-insecure-requests");```

或着也可以在由前端在html页面中添加meta:

```<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests" />```

或者在nginx配置中进行header的添加:

```server { listen 443; server_name www.example.com; error_log /logs/nginx/error.log; root /var/www/www.example.com; index index.php index.html index.htm; ssl on; ssl_certificate cert/test/test.pem; ssl_certificate_key cert/test/test.key; ssl_session_timeout 5m; #添加响应头 add_header Content-Security-Policy "upgrade-insecure-requests'"; location / { if (!-f $request_filename){ rewrite ^/(.*)$ /index.php?s=$1 last; break; } limit_except GET POST DELETE PUT { deny all; } } …… }```

从而让浏览器对页面中的所有http请求,都升级为https协议发送请求,从而省去了逐一修改url的协议的麻烦。

免责声明

本站提供的一切软件、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集整理,如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
如若转载,请注明出处:https://www.zxki.cn/2304.html

上一篇 2021-12-17 11:25
下一篇 2021-12-17 12:21

相关推荐

发表评论

为了防止灌水评论,登录后即可评论!

还没有评论,快来抢沙发吧!