背景:
項目開發中用到了微信小程序,但是服務器配置url必須是https,所以需要通過配置nginx的ssl模塊來支持https訪問,也就是說,要做一個網站域名為 dmsdbj.com 要求通過https://dmsdbj.com進行訪問.
ssl英文名為secure socket layer,安全套接字層。ssl是一種數字證書,它使用ssl協議在瀏覽器和web server之間建立一條安全通道,數據信息在client與server之間的安全傳輸.
前提:
1. 配置ssl模塊首先需要ca證書,ca證書可以自己手動頒發也可以在阿里云申請,本人在阿里云上申請的證書。(手動頒發可參考文章底部鏈接)
2. 默認情況下ssl模塊并未被安裝,如果要使用該模塊則需要在編譯nginx時指定–with-http_ssl_module參數.
阿里云購買ca證書
操作步驟:
一、下載ca證書
1. 登錄阿里云,選擇“控制臺”-“產品與服務”,在“安全(云盾)”一欄中選擇“ca證書服務(數據安全)”。
2.在已經購買好的證書點擊“下載”,在新打開的頁面上選擇“nginx/tengine”,點擊“下載證書for nginx”。
二、在nginx配置文件中安裝證書
文件說明:1. 證書文件“證書名稱.pem‘’,包含兩段內容,請不要刪除任何一段內容。2. 如果是證書系統創建的csr,還包含:證書私鑰文件“證書名稱.key”。( 1 ) 在nginx的配置文件所在的目錄下創建cert文件夾,并且將下載的全部文件拷貝到cert目錄中。如果申請證書時是自己創建的csr文件,請將對應的私鑰文件放到cert目錄下并且命名為“證書名稱.key”;
( 2 ) 打開 nginx 安裝目錄下 conf 目錄中的 nginx.conf 文件,找到:
#?https?server #?#server?{ #?listen?443; #?server_name?localhost; #?ssl?on; #?ssl_certificate?cert.pem; #?ssl_certificate_key?cert.key; #?ssl_session_timeout?5m; #?ssl_protocols?sslv2?sslv3?tlsv1; #?ssl_ciphers?all:!adh:!export56:rc4+rsa:+high:+medium:+low:+sslv2:+exp; #?ssl_prefer_server_ciphers?on; #?location?/?{ # # #} #}
( 3 ) 將其修改為 (以下屬性中ssl開頭的屬性與證書配置有直接關系,其它屬性請結合自己的實際情況復制或調整) :
server?{ ??listen?443; ??server_name?localhost; ??ssl?on; ??root?html; ??index?index.html?index.htm; ??ssl_certificate??cert/證書名稱.pem; ??ssl_certificate_key?cert/證書名稱.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?/?{ ????root?html; ????index?index.html?index.htm; ??} }
保存退出。
( 4 )重啟 nginx。
nginx?-s?reload
( 5 ) 通過 https 方式訪問您的站點,測試站點證書的安裝配置。 在瀏覽器中輸入https://dmsdbj.com,如下圖所示,則說明配置成功。
安裝過程中遇見的問題
錯誤一:
nginx: [emerg] unknown directive “ssl” in /usr/local/nginx/conf/nginx.conf:151
解決方案:
出現這種錯誤可能是兩種情況造成的:
情況一:配置文件格式不正確。
解決方法參考鏈接:
情況二:ssl模塊并未被安裝
默認情況下ssl模塊并未被安裝,如果要使用該模塊則需要在編譯nginx時指定–with-http_ssl_module參數,這種情況也會導致錯誤二的出現。
解決方案:
nginx缺少http_ssl_module模塊,編譯安裝的時候帶上–with-http_ssl_module配置就行了,但是現在的情況是我的nginx已經安裝過了,怎么添加模塊,其實也很簡單,往下看: 做個說明:我的nginx的安裝目錄是/usr/local/nginx這個目錄,我的源碼包在/usr/local/src/nginx-1.3.6目錄
(1)切換到源碼包:
cd?/root/nginx-1.13.6
(2)配置信息:
./configure?--prefix=/usr/local/nginx?--with-http_stub_status_module?--with-http_ssl_module
(3)配置完成后,運行make進行編譯,千萬不要進行make install,否則就是覆蓋安裝。
mark
(4)然后備份原有已經安裝好的nginx
cp?/usr/local/nginx/sbin/nginx?/usr/local/nginx/sbin/nginx.bak
(5)停止nginx,正常命令直接 nginx -s stop就可以
nginx?-s?stop
如果關不掉,就直接kill掉進程。ps aux | grep 進程名 查看進程占用的pid號。
ps?aux|grep?nginx
殺掉查出來的pid就可以了,kill -9 pid 命令用于終止進程。必須先kill掉root對應的pid才能進行下面的三個nobody的pid。
kill?-9?10922 kill?-9?28276 kill?-9?28277 kill?-9?28278
(6)將剛剛編譯好的nginx覆蓋掉原有的nginx
cp?./objs/nginx?/usr/local/nginx/sbin/
(7)啟動nginx
nginx
(8)通過下面的命令查看是否已經加入成功。
nginx?-v
錯誤二:
nginx: [emerg] the “ssl” parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:148
解決方案:
出現這種情況的解決方案參考錯誤一的第二種情況的解決方案即可。
錯誤三:
stoping nginx… nginx: [emerg] bio_new_file(“/usr/local/nginx/conf/cert/214291778530222.pem”) failed (ssl: error:02001002:system library:fopen:no such file or directory:fopen(‘/usr/local/nginx/conf/cert/214291778530222.pem’,’r’) error:2006d080:bio routines:bio_new_file:no such file) failed. use force-quit
解決方案:
這可能是證書路徑存放的位置不正確導致的,而且只要寫絕對路徑,就會報錯,無論windows還是linux。
將證書文件放到nginx.conf所在的目錄下即可。