window系統創建自簽名SSL證書進行https加密域名訪問


應用場景

公眾號只配置了https的url地址,用Nginx做反向代理時被服務攔截提示“此網站無法提供安全連接”

tips:

  1. 公眾號后台路徑區分http和https
  2. hosts文件和nginx配置在沒有SSL證書的情況下,無法代理https流量,所以需要申請自簽名證書

OpenSSL
github地址

https://github.com/openssl/openssl

官方文檔

https://www.openssl.org/

快捷安裝包地址

http://slproweb.com/products/Win32OpenSSL.html

下載window exe安裝包

安裝OpenSSL

同意,並下一步

img

記住安裝的文件夾路徑,待會會用到

img

下一步

img

下一步

img

下一步

下一步

img

下一步

img

安裝成功

生成SSL

打開安裝OpenSSL的文件夾,進入bin目錄

img

以管理員身份打開openssl.exe文件

img

tips:

  如果這個地方打不開,設置環境變量,如果能正常打開則下面的所有命令都不用加openssl。

      設置環境變量,例如工具安裝在C:\OpenSSL-Win64,則將C:\OpenSSL-Win64\bin;復制到Path中

 

打開命令行程序cmd(以管理員身份運行),運行以下命令:

 

 

    openssl genrsa -des3 -out server.key 2048

 

 

回車,再依次輸入2次密碼,直接輸入123465就行

完成后自動生成server.key文件

 

繼續使用key自簽名生成csr文件

輸入以下命令

 

  openssl req -new -key server.key -out server.csr

 

 

注意,會先輸入密碼,在輸入其他信息,最后再輸入密碼

參數說明

 

Country Name (2 letter code) [AU]:CN #國家
State or Province Name (full name) [Some-State]:Beijing #省
Locality Name (eg, city) []:Beijing #市
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ncda #公司
Organizational Unit Name (eg, section) []:IT #部門
Common Name (e.g. server FQDN or YOUR name) []:localhost #域名 這里需要輸入服務器的域名
Email Address []:kong.weisheng@nidec.com #郵箱

 

 

 到這里就生成了server.csr文件

刪除密碼
為了避免每次啟動服務器都要輸入SSL密碼,在這里生成一個不需要密碼的key

輸入以下命令

 

openssl rsa -in server.key -out server_no_passwd.key

 

輸入之前設置的密碼確認即可

 

 

 

 

繼續生成自簽名文件

 

openssl x509 -req -days 36500 -in server.csr -signkey server_no_passwd.key -out server.crt

 

到這里就可以了

 

 生成文件位於Administrator用戶文件夾下

 

 

 

雙擊server.crt安裝證書

img

 

設置hosts

img

開啟nginx

#user  nobody;
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    # 代理測試環境
    server {
        listen 80;
        server_name test.com;
        location / {
            # root html;
            # index index.html index.htm;
            proxy_set_header host $host;
            proxy_pass http://127.0.0.1:8095;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
     # test
    server {
        listen 443;
        # test
        server_name test.com;
        ssl on;
        # root html
        # index index.html index.htm;
        # SSL證書放在了同級目錄small文件夾下面
        ssl_certificate cert/test/server.crt;
        ssl_certificate_key cert/test/server_no_passwd.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        location / {
            # 指向當前的某個服務端口
            proxy_pass http://127.0.0.1:8095;
            #設置請求頭,並將頭信息傳遞給服務器端
            proxy_set_header Host $host;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
 

訪問https://test.com

img

繼續前往,雖然沒有小綠鎖,但是能正常訪問並獲取數據

img

 

 

 

END.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM