1. 腾讯云免费申请https证书,并下载
2. 按官方说明配置
2.1 获取证书
Nginx文件夹内获得SSL证书文件 1_www.domain.com_bundle.crt 和私钥文件2_www.domain.com.key,
- _www.domain.com_bundle.crt 文件包括两段证书代码
“—–BEGIN CERTIFICATE—–”和“—–END CERTIFICATE—–”,- _www.domain.com.key 文件包括一段私钥代码“
—–BEGIN RSA PRIVATE KEY—–”和“—–END RSA PRIVATE KEY—–”。
2.2 证书安装
将域名 www.domain.com 的证书文件1_www.domain.com_bundle.crt、私钥文件2_www.domain.com.key保存到同一个目录(可以直接保存在配置文件的目录)
更新Nginx根目录下 conf/nginx.conf 文件如下:
server {
listen 443;
server_name www.domain.com; #填写绑定证书的域名
ssl on;
ssl_certificate 1_www.domain.com_bundle.crt;
ssl_certificate_key 2_www.domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
#这一块按以前的配置
location / {
root html; #站点目录
index index.html index.htm;
}
}
修改完成的
server {
listen 443 ssl;
server_name www.liuyang2018.com; #填写绑定证书的域名
ssl on;
ssl_certificate 1_www.liuyang2018.com_bundle.crt;
ssl_certificate_key 2_www.liuyang2018.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
index index.php index.html;
root /yjdata/www/www/;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:10000;
include fastcgi.conf;
}
}
- 配置完成后,重启nginx。就可以使 https://www.liuyang2018.com 来访问了。
注:
配置文件参数 说明
listen 443 SSL访问端口号为443
ssl on 启用SSL功能
ssl_certificate 证书文件
ssl_certificate_key 私钥文件
ssl_protocols 使用的协议
ssl_ciphers 配置加密套件,写法遵循openssl标准
2.3 使用全站加密,http自动跳转https(可选)
3. 最后设置自动化加载https
对于用户不知道网站可以进行https访问的情况下,让服务器自动把http的请求重定向到https。
- 在服务器这边的话配置的话,可以在页面里加js脚本,
也可以在后端程序里写重定向,当然也可以在web服务器来实现跳转。 - Nginx是支持rewrite的(只要在编译的时候没有去掉pcre)
在http的server里增加rewrite ^(.*) https://$host$1 permanent;
server {
listen 80;
server_name localhost;
index index.php index.html;
root /yjdata/www/www/;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:10000;
include fastcgi.conf;
rewrite ^(.*) https://$host$1 permanent;
}
这样就可以实现80进来的请求,重定向为https了。