Nginx 服务器 SSL 证书安装部署
当我们访问博客的时候发现url前面总是带着不安全的字样,因此我们需要将http协议替换成https协议。
# 操作步骤
以腾讯云服务器为例。
- 下载需要安装的证书。
- 在弹出的“证书下载”窗口中选择Nginx, 单击并解压缩文件包到本地目录。
解压后,可获得相关类型的证书文件。其中包含zfprotectors.com_nginx文件夹:
zfprotectors.com_bundle.crt
:证书文件zfprotectors.com_bundle.pem
:证书文件(可忽略该文件)zfprotectors.com.csr
:CSR文件zfprotectors.com.key
:私钥文件
提示
CSR文件是申请证书时由个人上传或系统在线生成的,提供给CA机构。 安装时可忽略该文件。
将以获取的
zfprotectors.com_bundle.crt
证书文件和zfprotectors.com.key
私钥文件拷贝到/etc/nginx/cert
(若无cert文件夹,则自行创建即可)编辑 Nginx 中的
/etc/nginx/conf.d/blog.conf
(其中blog.conf为自行创建的文件)。
提示
由于版本问题,配置文件可能存在不同的写法。例如:Nginx 版本为 nginx/1.15.0 以上请使用 listen 443 ssl
代替 listen 443
和 ssl on
。
server {
#SSL 访问端口号为 443
listen 443 ssl;
#填写绑定证书的域名
server_name cloud.tencent.com;
#证书文件名称
ssl_certificate cloud.tencent.com_bundle.crt;
#私钥文件名称
ssl_certificate_key cloud.tencent.com.key;
ssl_session_timeout 5m;
#请按照以下协议配置
ssl_protocols TLSv1.2 TLSv1.3;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
location / {
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
#例如,您的网站运行目录在/etc/www下,则填写/etc/www。
root html;
index index.html index.htm;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 重启Nginx:
service nginx restart
,之后即可访问https了
# HTTP自动跳转HTTPS(可选)
当进行上述步骤处理后,会发现我们可以访问两个一个是http协议,一个是https协议。此时我们需要将访问http协议的域名转变成https。
在上述步骤的基础上,在blog.conf上修改成以下内容。
server {
listen 443 ssl;
#填写绑定证书的域名
server_name cloud.tencent.com;
#证书文件名称
ssl_certificate cloud.tencent.com_bundle.crt;
#私钥文件名称
ssl_certificate_key cloud.tencent.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
#例如,您的网站运行目录在/etc/www下,则填写/etc/www。
root html;
index index.html index.htm;
}
}
server {
listen 80;
#填写绑定证书的域名
server_name cloud.tencent.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
修改完成后,重启Nginx,之后再访问域名即可。
编辑 (opens new window)
上次更新: 2022/05/16, 09:48:09