CentOS7.6下Nginx配置Https

CentOS7.6下Nginx安装请参考博文:阿里云CentOS7.6下安装Nginx

现在为了网站更加安全,可以采用https访问,需要安装SSL证书,一般SSL证书都需要付费,但是也有免费的,我这里在阿里云申请了免费的DV证书。
SSL

在 产品->安全->SSL证书 下可以选购免费的DV证书
SSL

SSL

证书下载选择 Nginx,会得到2个文件xxx_river106.cn.key, xxx_river106.cn.pem。

配置Https前Nginx需要安装http_ssl_module(–with-http_ssl_module), 请参考Nginx安装博文。

修改Nginx配置文件

1
vim /usr/local/nginx/conf/nginx.conf

配置内容如下:

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
27
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
#监听443端口
listen 443;
#你的域名
server_name river106.cn;
ssl on;
#ssl证书的pem文件路径
ssl_certificate /root/cert/xxx_river106.cn.pem;
#ssl证书的key文件路径
ssl_certificate_key /root/cert/xxx_river106.cn.key;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name river106.cn;
#将请求转成https
return 301 https://$server_name$request_uri;
}
}

注意:如果是阿里云ECS服务器这里需要在安全组中开放443端口,防火墙开放443端口。

配置好后重启Nginx:

1
systemctl restart nginx.service

浏览器输入 https://river106.cn, 可以看到域名前的小锁标记(连接是安全的)
https://river106.cn


CentOS7.6下Nginx配置Https
https://river106.cn/posts/2f5344bf.html
作者
river106
发布于
2021年11月27日
许可协议