CentOS7-Nginx配置Let's-Encrypt-SSL证书
2020/11/10 8:24:42
本文主要是介绍CentOS7-Nginx配置Let's-Encrypt-SSL证书,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Let’s-Encrypt
为http站点添加https支持,需要从证书发行机构获取SSL/TLS 证书。常见的免费证书有两种:
- [Let’s-Encrypt],本文即将介绍,Let’s-Encrypt大法好。
- [caddy],原生支持 HTTP/2,自动创建 [Let’s Encrypt]证书,非常简单易用。
安装
yum install epel-release -y yum install certbot -y
配置
certbot certonly --webroot -w /www/html -d suncle.me -d www.suncle.me
--webroot
表示以webroot模式运行,此处我们不选择standalone模式-w /www/html
表示站点根目录在/www/html-d suncle.me -d www.suncle.me
表示为@主机和www主机生成共同的证书
按照提示操作成功后,提示:
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/suncle.me/fullchain.pem. Your cert will expire on 2017-06-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
证书自动更新
Let’s Encrypt 只有3个月的有效期,所以我们需要定时去更新证书。
可以通过运行:certbot renew --dry-run
来测试自动生成是否能够正常运行。如果正确执行,我们就可以通过以下命令更新证书:
certbot renew
如果要达到自动更新证书,可以借助crontab
或者systemd
定时执行上面的更新命令。Let’s Encrypt官方建议一天更新两次最好。因为证书没有到期之前是不会更新,因此即使一天执行两次也不会有什么影响。
# 每天3:00和19:00点更新证书 0 3,19 * * * certbot renew
具体执行时间可以参考以下crontab格式进行修改:
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
配置Nginx SSL证书
修改/usr/local/nginx/nginx.conf文件如下(最好先备份)
# Nginx以root用户启动 user root; # Nginx开启的进程数 worker_processes 2; events { # Nginx连接的最大个数 worker_connections 65535; } http { # 如果路径中出现通配符,mime.types可配置多个文件 include mime.types; # 默认文件类型 default_type application/octet-stream; # 日志格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; # 记录访问日志 access_log logs/access.log main; # 开启sendfile,提升文件传输效率 sendfile on; # 死链判断:客户端连接保持活动的超时时间 keepalive_timeout 65; #设置非安全连接永久跳转到安全连接 server{ listen 80; server_name www.suncle.me suncle.net blog.suncle.net; #告诉浏览器有效期内只准用 https 访问 add_header Strict-Transport-Security max-age=15768000; #永久重定向到 https 站点 return 301 https://$server_name$request_uri; } server { #启用 https, 使用 http/2 协议, nginx 1.9.11 启用 http/2 会有bug, 已在 1.9.12 版本中修复. listen 443 ssl http2; server_name www.suncle.me suncle.net blog.suncle.net; #告诉浏览器当前页面禁止被frame add_header X-Frame-Options DENY; #告诉浏览器不要猜测mime类型 add_header X-Content-Type-Options nosniff; root /www/html; #证书路径 ssl_certificate /etc/letsencrypt/live/suncle.me/fullchain.pem; #私钥路径 ssl_certificate_key /etc/letsencrypt/live/suncle.me/privkey.pem; #安全链接可选的加密协议 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #可选的加密算法,顺序很重要,越靠前的优先级越高. ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-RC4-SHA:!ECDHE-RSA-RC4-SHA:ECDH-ECDSA-RC4-SHA:ECDH-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:HIGH:!RC4-SHA:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!CBC:!EDH:!kEDH:!PSK:!SRP:!kECDH; #在 SSLv3 或 TLSv1 握手过程一般使用客户端的首选算法,如果启用下面的配置,则会使用服务器端的首选算法. ssl_prefer_server_ciphers on; #储存SSL会话的缓存类型和大小 ssl_session_cache shared:SSL:10m; #缓存有效期 ssl_session_timeout 60m; } }
以上配置文件nginx.conf中需要修改的字段主要有:
- server_name www.suncle.me suncle.net blog.suncle.net;
- ssl_certificate /etc/letsencrypt/live/suncle.me/fullchain.pem;
- ssl_certificate_key /etc/letsencrypt/live/suncle.me/privkey.pem;
> listen 443 ssl http2;
这一句中,如果Nginx编译安装的时候没有安装ngx_http_v2_module
模块,则需要先安装。或者不采用http2协议,直接listen 443 ssl
即可
保存配置,检查是否报错,重启Nginx
/usr/local/nginx/nginx -t /usr/local/nginx/nginx -s reload
Nginx 的 SSL 证书到此配置完成。
念念不忘,必有回响,小伙伴们帮我点个赞吧,非常感谢。
> 我是职场亮哥,YY高级软件工程师、四年工作经验,拒绝咸鱼争当龙头的斜杠程序员。
>
> 听我说,进步多,程序人生一把梭
>
> 如果有幸能帮到你,请帮我点个【赞】,给个关注,如果能顺带评论给个鼓励,将不胜感激。
本人所有文章、回答都与版权保护平台有合作,著作权归职场亮哥所有,未经授权,转载必究!
这篇关于CentOS7-Nginx配置Let's-Encrypt-SSL证书的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-17Nginx实用篇:实现负载均衡、限流与动静分离
- 2024-08-21宝塔nginx新增8022端口方法步骤-icode9专业技术文章分享
- 2024-08-21nginx配置,让ws升级为wss访问的方法步骤-icode9专业技术文章分享
- 2024-08-15nginx ws代理配置方法步骤-icode9专业技术文章分享
- 2024-08-14nginx 让访问带有/relid的地址返回404 ,例子 /relid-x-0.36-y-131.html-icode9专业技术文章分享
- 2024-08-14nginx 判断地址有/statics/的路径,指向到/home/html/statics/目录-icode9专业技术文章分享
- 2024-07-24解锁Nginx日志的宝藏:GoAccess——你的实时、交互式Web日志分析神器!
- 2024-03-29fail2ban nginx
- 2024-03-18install nginx proxy manager
- 2024-01-2444从零开始用Rust编写nginx,命令行参数的设计与解析及说明