配置站点使用 https,并且将 http 重定向至 https。
1. nginx 的 ssl 模块安装
查看 nginx 是否安装 http_ssl_module 模块。
?
1
$ /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
?
1
2
3
# 下载安装包到 src 目录
$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.14.1.tar.gz
解压安装包。
?
1
$ tar -zxvf nginx-1.14.1.tar.gz
配置 ssl 模块。
?
1
2
$ cd nginx-1.14.1
$ ./configure –prefix=/usr/local/nginx –with-http_ssl_module
使用 make 命令编译(
使用make install
会重新安装nginx),此时当前目录会出现 objs 文件夹。
用新的 nginx 文件覆盖当前的 nginx 文件。
?
1
$ cp ./objs/nginx /usr/local/nginx/sbin/
再次查看安装的模块(configure arguments: –with-http_ssl_module说明ssl模块已安装)。
?
1
$ /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 文件夹存放证书文件。
?
1
2
$ cd /usr/local/nginx
$ mkdir cert
将这两个文件上传至服务器的 cert 目录里。
这里使用 mac 终端上传至服务器的 scp 命令(这里需要新开一个终端,不要使用连接服务器的窗口):
?
1
2
$ 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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
?
1
2
3
4
5
server {
listen 80;
server_name baidu.com www.baidu.com;
return 301 https://$server_name$request_uri;
}
4. 重启 nginx
?
1
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果 80 端口被占用,用kill [id]来结束进程:
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
?
1
2
# 结束 80 端口进程
$ kill 21307
再次重启 nginx :
无信息提示就成功啦~
到此这篇关于Nginx 配置 HTTPS 完整过程的文章就介绍到这了,更多相关Nginx 配置 HTTPS内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_37264997/article/details/84525444
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。