SSL 证书安装部署
大约 1 分钟
server {
listen 80;
listen [::]:80;
server_name example.com;
#将http的域名请求转成https
return 301 https://$host$request_uri;
}
server {
listen 80;
listen [::]:80;
server_name xxx.example.com;
#将http的域名请求转成https
return 301 https://$host$request_uri;
}
server {
#SSL 默认访问端口号为 443
listen 443 ssl;
#请填写绑定证书的域名
server_name example.com;
#请填写证书文件的相对路径或绝对路径
ssl_certificate /etc/nginx/conf.d/example.com_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key /etc/nginx/conf.d/example.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 / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /xxx {
alias /usr/share/nginx/xxx;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
server {
#SSL 默认访问端口号为 443
listen 443 ssl;
# 开启 http2
http2 on;
#请填写绑定证书的域名
server_name xxx.example.com;
#请填写证书文件的相对路径或绝对路径
ssl_certificate /etc/nginx/conf.d/xxx.example.com_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key /etc/nginx/conf.d/xxx.example.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;
root /usr/share/nginx/xxx;
location / {
index index.html;
}
}
nginx 配置 https 需要提前准备好证书,在配置 ssl_certificate
和 ssl_certificate_key
时,这两个配置的是证书目录,可以选择使用相对路径和绝对径路,默认的相对路径是 nginx 的配置根目录 /etc/nginx
。
注意:nginx 配置文件书写完毕之后,需要设置防火墙打开 443 端口,如果使用的是 docker 可能需要创新创建容器。