nginx安裝SSL證書的正確方法

nginx安裝SSL證書的正確方法

前提條件:

已經(jīng)生成了以下文件:

domain.com.crt???(域名證書)?有的證書可能是cer?或?pem?或其他后綴名,都可以

domain.com.key???(私鑰文件)

(相關(guān)教程:nginx教程

配置nginx

找到站點(diǎn)的配置文件,在server中添加443端口監(jiān)聽和證書文件引用

server?{ ????listen?80; ????#監(jiān)聽443端口(必須) ????listen?443?ssl; ???? ????server_name?domain.com?www.domain.com; ????index?index.html?index.php?index.htm; ????root?/www/wwwroot/domain.com;  ????#引用證書(必須,放在conf/ssl目錄下可以用相對(duì)路徑,其他位置用絕對(duì)路徑) ????ssl_certificate?????ssl/domain.com.crt; ????ssl_certificate_key?ssl/domain.com.key;  ????#協(xié)議優(yōu)化(可選,優(yōu)化https協(xié)議,增加安全性) ????ssl_protocols?TLSv1?TLSv1.1?TLSv1.2; ????ssl_ciphers?ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ????ssl_prefer_server_ciphers?on; ????ssl_session_cache?shared:SSL:10m; ????ssl_session_timeout?10m;  ????#其他的配置信息··· }

以上為最基本的配置,其他參數(shù)請(qǐng)根據(jù)生產(chǎn)環(huán)境需要添加

安裝后重啟nginx使其生效

如果重啟nginx

失敗使用如下命令檢測(cè)nginx是否支持SSL

nginx?-V

如果有顯示?–with-http_ssl_module 表示已編譯openssl,支持安裝ssl,仔細(xì)檢查配置文件
如果沒有安裝請(qǐng)下載nginx源碼重新編譯

./configure?--with-http_stub_status_module?--with-http_ssl_module make?&&?make?install

強(qiáng)制跳轉(zhuǎn)HTTPS

在配置文件中添加跳轉(zhuǎn)代碼

server?{ ????#站點(diǎn)和SSL的配置信息···  ????#自動(dòng)跳轉(zhuǎn)到HTTPS(可選,和下面的部分域名跳轉(zhuǎn)不能同時(shí)使用) ????if?($server_port?=?80){ ????????rewrite?^(/.*)$?https://$host$1?permanent; ????}  ????#綁定域名較多,只讓部分域名跳轉(zhuǎn)(根據(jù)情況選用,和上面的全部跳轉(zhuǎn)不能同時(shí)使用) ????set?$redirect_https?1; ????if?($server_port?=?80)?{ ????????set?$redirect_https?"${redirect_https}2"; ????} ????if?($http_host?=?'abc.com')?{ ????????set?$redirect_https?"${redirect_https}3"; ????} ????if?($http_host?=?'cde.com')?{ ????????set?$redirect_https?"${redirect_https}3"; ????} ????if?($redirect_https?=?"123")?{ ????????#當(dāng)前域名跳轉(zhuǎn) ????????rewrite?^(.*)$?https://$host$1?permanent; ????????#可以跳轉(zhuǎn)到指定的域名 ????????#rewrite?^(.*)$?https://www.abcde.com$1?permanent; ????} }

以上就是

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊11 分享