Nginx导入免费个人电子证书

简介
随着越来越多的网站全站支持https,相应的电子证书机构也多了。有好几家都提供面向个人的免费电子证书了。如:七牛,亚马逊,Let's Encrypt等。 本文主要介绍使用Let's Encrypt快速让自己的个人网站支持https.
环境
OS: 阿里云主机 Web Server: Nginx
前提条件
  • 做好相关的域名解析
  • 确保云主机上的80443端口没有被占用
电子证书设置部分
# 安装certbot
$ cd /usr/local
$ git clone https://github.com/certbot/certbot
$ cd /usr/local/certbot
$ sudo ./certbot-auto -debug

# 停止nginx
$ sudo service nginx stop

# 生成ssl电子证书。选择nginx支持,根据提示输入自己的域名
$ sudo ./certbot-auto certonly

# nginx重启
$ sudo service nginx restart
Nginx设置部分
选择自己前面输入的域名对应的配置文件并修改
server {
        listen 80;

        server_name www.domain.com domain.com;

        return 301 https://$host$request_uri;
}


server {
        listen 443 ssl;
        server_name www.domain.com domain.com;

        root /path/to/webroot;
        index index.php index.html index.htm;

        ssl_certificate /etc/letsencrypt/live/www.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain.com/privkey.pem;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_max_temp_file_size 0;

        ssl_session_timeout 5m;

        if (!-e $request_filename)
        {
                rewrite ^(.+)$ /index.php?q=$1 last;
        }

        location / {
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

准备完毕,重启nginx!
$ sudo service ngninx restart
电子证书自动更新
  • crontab当前运行的任务确认
$ crontab -l
  • crontab追加证书更新定期任务
$ crontab -e

# 每月1号1点更新电子证书
0 1 1 * * /usr/local/certbot/certbot-auto renew --force-renewal && service nginx restart
效果图
nginx https 浏览器效果图
CA认证Secure绿色标签出来了

查看原文:http://www.huuinn.com/archives/287
更多技术干货:风匀坊
关注公众号:风匀坊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值