https协议一看就觉得逼格高端。很早时候就想做一个来玩玩了。
现在有机会了
感谢
咖菲猫-李常明笔记 的
certbot在Centos7上配置合法签名证书,实现nginx的https访问
本文其实就是 咖菲猫-李常明笔记 的笔记的读书笔记而已。
certbot的官网地址:
https://certbot.eff.org/
上面其实已经有相关指导了,
我选择的是 nginx + centos
看到下面的指导。。
我翻译一下算了:
因为在centos里面没有可直接执行的打包过的cert bot 软件,所以目前的做法是,复制一份copy在本地编译安装。
wget https://dl.eff.org/certbot-auto
sudo mv certbot-auto /usr/local/bin/certbot-auto
sudo chown root /usr/local/bin/certbot-auto
sudo chmod 0755 /usr/local/bin/certbot-auto
Certbot自带针对nginx的插件,可以针对众多平台自动执行ssl证书安装过程。
sudo /usr/local/bin/certbot-auto --nginx
如果你想体验手动的快感,手动修改配置,那么,好,执行命令时候添加参数"certonly"
$ sudo /usr/local/bin/certbot-auto --nginx certonly
然后后续过程请参考:
https://certbot.eff.org/docs/
说回自动安装的过程吧,
注意,要求填写域名时候,只能填写单域名不要填写范域名,是错的,会这样提示:
正确的是:
IMPORTANT NOTES:
- Unable to install the certificate
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/www.freedom_plus.club/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/www.freedom_plus.club/privkey.pem
Your cert will expire on 2019-09-04. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again with the "certonly" option. To non-interactively renew *all*
of your certificates, run "certbot-auto renew"
已经生成以后,那么配置那一部分这样来
参考之前的文章
nginx+ca+https设置
首先,看看生成的key都有哪些:
ls /etc/letsencrypt/live/www.freedom_plus.com/
看到:
好了,在nginx.conf里面添加
参考: 咖菲猫-李常明笔记
的配置
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name kafeimao.com;
ssl on;
ssl_certificate "/etc/letsencrypt/live/kafeimao.com/fullchain.pem";
ssl_certificate_key "/etc/letsencrypt/live/kafeimao.com/privkey.pem";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html/kafeimao.com;
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
注意,为了原本的80端口即http的普通协议能够转换到https这边来,可以添加这一条规则:
## http move to https
server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
好了,不过我们有些文件,譬如静态资源,js gif等等是要直接访问硬盘,不需要代理的,这时候这样做
## https里面直接代理静态资源
server {
listen 443;
server_name domain.com;
location ~/(javascript|css|images|res|static|statics|templates){
root /home/root/web/static/;
expires 7d;
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
## 下面代理转发php网站或java网站
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/;
}
}
首先,尝试执行:
sudo /usr/local/bin/certbot-auto renew --dry-run
如果执行成功,那么就可以用:
/usr/local/bin/certbot-auto renew
来手动更新,推荐用下面代码每两天检查一下更新:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew
下面添加定时任务:
crontab -e
打开定时任务编辑器
添加内容:
0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew
保存,然后使用:
crontab -l -u root
查看是否添加了,然后,
/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload
请重启定时任务