網站首頁 編程語言 正文
配置站點使用 https,并且將 http 重定向至 https。
1. nginx 的 ssl 模塊安裝
查看 nginx 是否安裝 http_ssl_module
模塊。
$ /usr/local/nginx/sbin/nginx -V
如果出現 configure arguments: --with-http_ssl_module
, 則已安裝(下面的步驟可以跳過,進入 nginx.conf
配置)。
下載 nginx 安裝包, nginx官網1.14.1穩定版本tar.gz包。
百度網盤下載地址:
鏈接: 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
會重新安裝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
…
configure arguments: –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 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 [本地文件路徑,可以直接拖文件至終端里面] [<服務器登錄名>@<服務器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
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? LISTEN 21307/nginx: master
# 結束 80 端口進程 $ kill 21307
再次重啟 nginx :
無信息提示就成功啦~
原文鏈接:https://blog.csdn.net/weixin_37264997/article/details/84525444
相關推薦
- 2022-12-13 深入了解Rust的生命周期_Rust語言
- 2022-04-15 利用Python實現數值積分的方法_python
- 2023-01-23 C#實現鼠標左右鍵切換效果_C#教程
- 2022-04-19 前端如何使用webpack優化性能
- 2022-10-05 使用?Swift?Package?插件生成代碼的示例詳解_Swift
- 2022-10-21 Go?Ticker?周期性定時器用法及實現原理詳解_Golang
- 2023-01-13 Python實現數字小寫轉大寫的示例詳解_python
- 2022-04-16 python如何導入自己的模塊_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支