windwos下nginx 配置https並http強制跳轉https
一、首先配置證書文件
申請證書文件,這里就不做詳細過程了,直接看證書文件結果。
這是兩個證書的關鍵文件

打開ngxin下conf文件夾下的 ngxin.conf 配置文件,往下翻,找到 server{listen:443;} 這一部分,443 端口就是專門給 https用的。

將前面的 # 去掉注釋,記得要在服務器安全組中打開443端口。
這樣就可以 用https 訪問項目了,但是 可能會碰到問題
因為這里已經是443 端口運行了,可能會影響到你后台的端口,導致接口無法成功運行。
那么就可以利用 端口重定向;

這么就沒問題了。
二、如何 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了。
