修改nginx_conf部分配置
修改 nginx.conf 部分配置
配置 http 转成 https
server {
listen 80;
server_name www.zervan.cn;
#rewrite ^(.*)$ https://$host$1 permanent;
rewrite ^/(.*)$ http://www.zervan.cn/$1 permanent;
}
配置 ssl 证书 给原有注释的 # HTTPS server 拷贝一份
# HTTPS server
#
server {
listen 443 ssl;
server_name www.zervan.cn;
ssl_certificate /etc/nginx/ssl/zervan.cn.pem;
ssl_certificate_key /etc/nginx/ssl/zervan.cn.key;
ssl on;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
sudo systemctl restart nginx
1、查看nginx安装目录
输入命令
# ps -ef | grep nginx
返回结果包含安装目录
root 2662 1 0 07:12 ? 00:00:00 nginx: master process /usr/sbin/nginx
2、查看nginx.conf配置文件目录
输入命令
# nginx -t
返回结果包含配置文件目录
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 4.编辑Nginx配置文件(nginx.conf)
编辑Nginx配置文件(nginx.conf),修改与证书相关的配置。
4.1 执行以下命令,打开配置文件。
vim /usr/local/nginx/conf/nginx.conf
复制
4.2 按i键进入编辑模式。
4.3 在配置文件中定位到HTTP协议代码片段(http{}),并在HTTP协议代码里面添加以下server配置(如果server配置已存在,按照以下注释内容修改相应配置即可)。 使用示例代码前,请注意替换以下内容: haiyong.site:替换成证书绑定的域名 (opens new window)。 fullchain.pem:替换成步骤3中上传的证书文件的名称。 private.key:替换成步骤3中上传的证书私钥文件的名称。
#以下属性中,以ssl开头的属性表示与证书配置有关。
server {
listen 443 ssl;
#配置HTTPS的默认访问端口为443。
#如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
#如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
server_name haiyong.site;
root html;
index index.html index.htm;
ssl_certificate cert/fullchain.pem;
ssl_certificate_key cert/private.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的加密套件的类型。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; #表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议。
ssl_prefer_server_ciphers on;
location / {
root /var/www/html; #Web网站程序存放目录。
index index.html index.htm;
}
ssl_certificate /etc/nginx/ssl/zervan.cn.pem;
ssl_certificate_key /etc/nginx/ssl/zervan.cn.key;
}
复制
4.4 可选:设置HTTP请求自动跳转HTTPS。 如果您希望所有的HTTP访问自动跳转到HTTPS页面,则可以在需要跳转的HTTP站点下添加以下rewrite语句。
注意:以下代码片段需要放置在 nginx.conf文件中 server{} 代码段后面,即设置HTTP请求自动跳转HTTPS后,nginx.conf 文件中会存在两个server {}代码段。
server {
listen 80;
server_name haiyong.site; #需要将haiyong.site替换成证书绑定的域名。
rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
location / {
index index.html index.htm;
}
}
复制