1. 获取免费的 CA 证书。
2. 然后填写信息申请
3. 下载对应证书
4. 上传证书到你的服务器
cd usr/local/nginx/conf/vhost
mkdir cert
将.key和.pem文件上传到cert文件夹
5. 配置 nginx(最好把之前 80 端口的配置备份一份)
server
{
listen 443 ssl;
server_name www.cfun.vip;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/blog_vue/public;
ssl_certificate cert/cfun.pem;
ssl_certificate_key cert/cfun.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;
include rewrite/y.conf;
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/y.log;
}
server {
listen 80;
server_name www.cfun.vip;
access_log /home/wwwlogs/y.log;
rewrite ^(.*)$ https://$host$1 permanent; #把http的域名请求转成https
}
}
Nginx 支持 rewrite 功能。若您在编译时没有去掉 pcre,您可在 HTTP 的 server 中增加
rewrite ^(.*) https://$host$1 permanent;
即可将默认 80 端口的请求重定向为 HTTPS。
6. 重启 nginx,访问域名:www.cun.vip 会自动跳转 https 安全访问;如图
7. 总结:第一次听别人说配安全证书,心生羡慕,有一种来自菜鸟的仰慕。当你接触了才发现,原来如此。。。哈哈。多动手,多思考。Believe in yourself, everything is the beginning of the best!