nginx如何防止ssl證書(shū)過(guò)期?
nginx配置免費(fèi)SSL證書(shū)及證書(shū)定時(shí)更新
環(huán)境 contos 6,證書(shū)發(fā)行Let's Encrypt
證書(shū)生成前提是域名是可用的,即已經(jīng)備案通過(guò)并且有DNS解析到了具體IP
1、安裝epel,
>yum install epel-release
2、下載certbot證書(shū)生成工具certbot-auto
>wget https://dl.eff.org/certbot-auto --no-check-certificate
3、安裝工具的依賴
>chmod +x certbot-auto >./certbot-auto -n
4、生成證書(shū)
單域名:
>./certbot-auto certonly --email my@163.com --agree-tos --no-eff-email --webroot -w /usr/local/nginx/html/xue/ -d www.xue37.cn
注意:替換郵箱、網(wǎng)站目錄和域名
多域名:
>./certbot-auto certonly --email my@163.com --agree-tos --no-eff-email --webroot -w /usr/local/nginx/html/xue/ -d www.xue37.cn -d xue37.cn
證書(shū)生成在/etc/letsencrypt/live/www.xue37.cn/目錄下(具體生成地址執(zhí)行完命令有提示信息)
5、證書(shū)延期(因?yàn)樽C書(shū)有效期為90天)
certbot-auto工具支持證書(shū)延期操作,因此可以使用crontab定時(shí)任務(wù)定時(shí)自動(dòng)延期
>0 3 * * * /root/certbot-auto renew --disable-hook-validation --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
每天3點(diǎn)進(jìn)行證書(shū)延期,crontab表達(dá)式自己可以百度
注意:
自己可以先單獨(dú)執(zhí)行一下:
/root/certbot-auto renew --disable-hook-validation --renew-hook "/usr/local/nginx/sbin/nginx -s reload"
我這里提示The following certs are not due for renewal yet,表示證書(shū)未到期,沒(méi)有其他錯(cuò)誤。因此為了防止證書(shū)失效時(shí)間過(guò)久,這里可以設(shè)置為每天都進(jìn)行延期操作
6、nginx增加證書(shū)配置
server { listen 443 ssl; server_name www.xue37.cn; ##這里是你的域名 ssl_certificate /etc/letsencrypt/live/www.xue37.cn/fullchain.pem; #前面生成的證書(shū),改一下里面的域名就行 ssl_certificate_key /etc/letsencrypt/live/www.xue37.cn/privkey.pem; #前面生成的密鑰,改一下里面的域名就行 ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; access_log /data/application/logs/xue.access.log main; location ^~ /bot { proxy_pass http://xue-server; include proxy-params.conf; } location / { root html/xue; index index.html index.htm; } location = /50x.html { root html; } }
7、設(shè)置80端口301到443
修改nginx配置:
server { listen 80; server_name localhost; location /.well-known/ { add_header Content-Type 'text/plain;'; root /usr/local/nginx/html/xue; } location / { return 301 https://www.xue37.cn$request_uri; } }
注意:nginx修改后需要重啟:/usr/local/nginx/sbin/nginx -s reload
注意:nginx配置需要處理
location ~ /. { deny all; }
這段配置刪掉或注釋掉或在這段配置前面加上(如果沒(méi)有這段配置請(qǐng)忽略)
location ~ /.well-known { allow all; }