Nginx?配置?HTTPS的詳細(xì)過(guò)程
配置站點(diǎn)使用 https,并且將 http 重定向至 https。
1. nginx 的 ssl 模塊安裝
查看 nginx 是否安裝 http_ssl_module
模塊。
$ /usr/local/nginx/sbin/nginx -V
如果出現(xiàn) configure arguments: --with-http_ssl_module
, 則已安裝(下面的步驟可以跳過(guò),進(jìn)入 nginx.conf
配置)。
下載 nginx 安裝包, nginx官網(wǎng)1.14.1穩(wěn)定版本tar.gz包。
百度網(wǎng)盤(pán)下載地址:
鏈接: https://pan.baidu.com/s/1_rMCsr3Dtyohoh3CxbcZ9w 提取碼: p3tn
# 下載安裝包到 src 目錄 $ cd /usr/local/src $ wget http://nginx.org/download/nginx-1.14.1.tar.gz
解壓安裝包。
$ tar -zxvf nginx-1.14.1.tar.gz
配置 ssl 模塊。
$ cd nginx-1.14.1 $ ./configure --prefix=/usr/local/nginx --with-http_ssl_module
- 使用
make
命令編譯(使用make install
會(huì)重新安裝nginx),此時(shí)當(dāng)前目錄會(huì)出現(xiàn)objs
文件夾。 - 用新的 nginx 文件覆蓋當(dāng)前的 nginx 文件。
$ cp ./objs/nginx /usr/local/nginx/sbin/
再次查看安裝的模塊(configure arguments: --with-http_ssl_module
說(shuō)明ssl模塊已安裝)。
$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.14.1
…
configure arguments: –with-http_ssl_module
2. ssl 證書(shū)部署
- 下載申請(qǐng)好的 ssl 證書(shū)文件壓縮包到本地并解壓(這里是用的 pem 與 key 文件,文件名可以更改)。
- 在 nginx 目錄新建 cert 文件夾存放證書(shū)文件。
$ cd /usr/local/nginx $ mkdir cert
- 將這兩個(gè)文件上傳至服務(wù)器的 cert 目錄里。
這里使用 mac 終端上傳至服務(wù)器的 scp 命令(這里需要新開(kāi)一個(gè)終端,不要使用連接服務(wù)器的窗口):
$ scp /Users/yourname/Downloads/ssl.pem root@xxx.xx.xxx.xx:/usr/local/nginx/cert/ $ scp /Users/yourname/Downloads/ssl.key root@xxx.xx.xxx.xx:/usr/local/nginx/cert/
scp [本地文件路徑,可以直接拖文件至終端里面] [<服務(wù)器登錄名>@<服務(wù)器IP地址>:<服務(wù)器上的路徑>]
3. nginx.conf 配置
編輯 /usr/local/nginx/conf/nginx.conf
配置文件:
- 配置 https server。
注釋掉之前的 http server 配置,新增 https server:
server { # 服務(wù)器端口使用443,開(kāi)啟ssl, 這里ssl就是上面安裝的ssl模塊 listen 443 ssl; # 域名,多個(gè)以空格分開(kāi) server_name baidu.com www.baidu.com; <span class="hljs-comme
將 http 重定向 https
server { listen 80; server_name baidu.com www.baidu.com; return 301 https://$server_name$request_uri; }
4. 重啟 nginx
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果 80 端口被占用,用kill [id]
來(lái)結(jié)束進(jìn)程:
# 查看端口使用 $ netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 21307/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0: LISTEN 3072/sshd
tcp 0 0 0.0.0.0:443 0.0.0.0
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。