1. 程式人生 > 其它 >Nginx 配置 HTTPS 完整過程

Nginx 配置 HTTPS 完整過程

技術標籤:nginx大廠任性挑Nginx配置HTTPS

原文格式更清晰:https://blog.csdn.net/weixin_37264997/article/details/84525444

配置站點使用 https,並且將 http 重定向至 https。

1. nginx 的 ssl 模組安裝

  • 檢視 nginx 是否安裝http_ssl_module模組。
$ /usr/local/nginx/sbin/nginx -V

如果出現configure arguments: --with-http_ssl_module, 則已安裝(下面的步驟可以跳過,進入nginx.conf配置)。

# 下載安裝包到 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會重新安裝nginx),此時當前目錄會出現objs資料夾。
  • 用新的 nginx 檔案覆蓋當前的 nginx 檔案。
$ cp ./objs/nginx /usr/local/nginx/sbin/
  • 再次檢視安裝的模組(configure arguments: --with-http_ssl_module說明ssl模組已安裝)。
$ /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.14.1

configurearguments: –with-http_ssl_module

2. ssl 證書部署

  • 下載申請好的 ssl 證書檔案壓縮包到本地並解壓(這裡是用的 pem 與 key 檔案,檔名可以更改)。
  • 在 nginx 目錄新建 cert 資料夾存放證書檔案。
$ cd /usr/local/nginx
$ mkdir cert
  • 將這兩個檔案上傳至伺服器的 cert 目錄裡。
    這裡使用 mac 終端上傳至伺服器的 scp 命令(這裡需要新開一個終端,不要使用連線伺服器的視窗):
$ scp /Users/yourname/Downloads/ssl.pem [email protected]:/usr/local/nginx/cert/
$ scp /Users/yourname/Downloads/ssl.key [email protected]:/usr/local/nginx/cert/

scp [本地檔案路徑,可以直接拖檔案至終端裡面] [<伺服器登入名>@<伺服器IP地址>:<伺服器上的路徑>]

3.nginx.conf配置

編輯/usr/local/nginx/conf/nginx.conf配置檔案:

  • 配置 https server。
    註釋掉之前的 http server 配置,新增 https server:
server {
    # 伺服器埠使用443,開啟ssl, 這裡ssl就是上面安裝的ssl模組
    listen       443 ssl;
    # 域名,多個以空格分開
    server_name  baidu.com www.baidu.com;
<span class="hljs-comment"># ssl證書地址</span> <span class="hljs-attribute">ssl_certificate</span> /usr/local/nginx/cert/ssl.pem; <span class="hljs-comment"># pem檔案的路徑</span> <span class="hljs-attribute">ssl_certificate_key</span> /usr/local/nginx/cert/ssl.key; <span class="hljs-comment"># key檔案的路徑</span> <span class="hljs-comment"># ssl驗證相關配置</span> <span class="hljs-attribute">ssl_session_timeout</span> <span class="hljs-number">5m</span>; <span class="hljs-comment">#快取有效期</span> <span class="hljs-attribute">ssl_ciphers</span> ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; <span class="hljs-comment">#加密演算法</span> <span class="hljs-attribute">ssl_protocols</span> TLSv1 TLSv1.<span class="hljs-number">1</span> TLSv1.<span class="hljs-number">2</span>; <span class="hljs-comment">#安全連結可選的加密協議</span> <span class="hljs-attribute">ssl_prefer_server_ciphers</span> <span class="hljs-literal">on</span>; <span class="hljs-comment">#使用伺服器端的首選演算法</span> <span class="hljs-attribute">location</span> / { <span class="hljs-attribute">root</span> html; <span class="hljs-attribute">index</span> index.html index.htm; } 

}

  • 將 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]來結束程序:

# 檢視埠使用
$ netstat -lntp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp000.0.0.0:800.0.0.0:LISTEN21307/nginx:master
tcp000.0.0.0:220.0.0.0:LISTEN3072/sshd
tcp000.0.0.0:4430.0.0.0