windwos下nginx 配置https并http强制跳转https

时间:2023-03-09 03:07:33
windwos下nginx  配置https并http强制跳转https

windwos下nginx  配置https并http强制跳转https

一、首先配置证书文件

申请证书文件,这里就不做详细过程了,直接看证书文件结果。

这是两个证书的关键文件

windwos下nginx  配置https并http强制跳转https

打开ngxin下conf文件夹下的 ngxin.conf 配置文件,往下翻,找到 server{listen:443;} 这一部分,443 端口就是专门给 https用的。

windwos下nginx  配置https并http强制跳转https

将前面的 # 去掉注释,记得要在服务器安全组中打开443端口。

这样就可以 用https 访问项目了,但是 可能会碰到问题

因为这里已经是443 端口运行了,可能会影响到你后台的端口,导致接口无法成功运行。

那么就可以利用 端口重定向;

windwos下nginx  配置https并http强制跳转https

这么就没问题了。

二、如何 http自动转向https呢,现在是 https和http 都可以访问。

Nginx版本

在配置80端口的文件里面,写入以下内容即可。

server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent; location / {
root html;
index index.html index.htm;
}

javascript单独页面通用代码段:以下方法较适合做seo搜索或指定某一个子页单独https

在需要强制为https的页面上加入以下代码进行处理

<script type="text/javascript">
var url = window.location.href;
if (url.indexOf("https") < 0) {
url = url.replace("http:", "https:");
window.location.replace(url);
}
</script>

这样,当你输入 域名进行访问的时候,会自动跳转到https了。