操作场景
本文档指导您如何在 Nginx 服务器中安装 SSL 证书。
说明
本文档以证书名称 blog.chaosaigc.com 为例。
Nginx 版本以 nginx/1.24.0 为例。
当前服务器的操作系统为 CentOS 7,由于操作系统的版本不同,详细操作步骤略有区别。
安装 SSL 证书前,请您在 Nginx 服务器上开启 HTTPS 默认端口 443,避免证书安装后无法启用 HTTPS。
SSL 证书文件上传至服务器。
操作步骤
证书安装
将证书上传至服务器/etc/nginx/cert/目录中
可在 HTTP 的 server 中增加 return 301 https://$host$request_uri;
,即可将默认80端口的请求重定向为 HTTPS。
由于版本问题,配置文件可能存在不同的写法。例如:Nginx 版本为 nginx/1.15.0 以上请使用 listen 443 ssl 代替 listen 443 和 ssl on。
# cat /etc/nginx/conf.d/wordpress.conf
server {
#SSL 默认访问端口号为 443
listen 443 ssl;
#请填写绑定证书的域名
server_name blog.chaosaigc.com;
#请填写证书文件的相对路径或绝对路径
ssl_certificate cert/blog.chaosaigc.com_bundle.crt;
#请填写私钥文件的相对路径或绝对路径
ssl_certificate_key cert/blog.chaosaigc.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;
index index.html index.htm index.php;
root /usr/share/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}
#HTTP 自动跳转 HTTPS 的安全配置,将默认80端口的请求重定向为 HTTPS
#在 HTTP 的 server 中增加 return 301 https://$host$request_uri;
server {
listen 80;
#请填写绑定证书的域名
server_name blog.chaosaigc.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}
检查配置文件是否有问题
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新加载配置
# nginx -s reload
配置完成之后,打开浏览器通过https://blog.chaosaigc.com
进行访问 ### 访问测试
Comments | NOTHING