ituser - 小小IT人遨游大大IT界

Nginx配置SSL

2020-04-15 15:40:32 142次浏览


更改Nginx安装目录/conf/nginx.conf文件。
或者新建SSL.conf(ssl配置文件)
找到以下配置信息:
# HTTPS server
  server {
  listen 443;
  server_name localhost;
  ssl on;
  ssl_certificate cert.pem;
  ssl_certificate_key cert.key;
  ssl_session_timeout 5m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  ssl_prefer_server_ciphers on;
  location / {
按照下文中注释内容修改nginx.conf文件:
# 以下属性中以ssl开头的属性代表与证书配置有关,其他属性请根据自己的需要进行配置。
server {
listen 443 ssl;   #SSL协议访问端口号为443
server_name localhost;  #将localhost修改为您证书绑定的域名,_ 代表接收所有域名
root html; #根据需要填写
index index.html index.htm; #根据需要填写
ssl_certificate cert/domain name.pem;   #将domain name.pem替换成您证书的全路径文件名。
ssl_certificate_key cert/domain name.key;   #将domain name.key替换成您证书的密钥全路径文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   #使用该协议进行配置。
ssl_prefer_server_ciphers on;   
location / {
root html;   #站点目录。
index index.html index.htm;   
}
} 
保存nginx.conf文件后退出。
执行以下命令重启Nginx服务器
nginx -s stop
nginx -s start
设置HTTP请求自动跳转HTTPS。
在需要跳转的HTTP站点下添加以下rewrite语句,实现HTTP访问自动跳转到HTTPS页面。
server {
 listen 80;
 server_name localhost;   #将localhost修改为您证书绑定的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent;   #将所有http请求通过rewrite重定向到https。
 location / {
index index.html index.htm;
}
}

centos6重启nginx:service nginx restart

centos7重启nginx:systemctl restart nginx

说明

所有内容来及个人经验和互联网摘取,如有雷同纯属巧合,如有冒犯,欢迎留言,分享即快乐,感谢互联网中每一位懂得分享的ituser!QQ群:127331971(备注:ituser.cn)