Nginx 实现 HTTPS(基于 Let's Encrypt 的免费证书)
SSL / TLS加密会为您的用户带来更高的搜索排名和更好的安全性。
Let’s Encrypt 是一个认证机构(CA)。它可以提供免费证书,并且已经被大多数浏览器所信任。另外,通过工具 Certbot 可以让我们完全自动化证书的安装和更新。
安装证书的前提条件:
安装服务器(这里用 NGINX)。
注册域名。
创建一个DNS记录,将域名和服务器的 IP 地址相关联。
记得安装完成后,防火墙需要打开 443 端口,否则无法访问!!!
1. 安装 Let’s Encrypt 客户端
所有的证书相关的操作,都可以通过 Certbot 软件实现。
注意:HTTPS 作为重要的基础服务,一旦出问题就需要立刻把软件更新到官网提供的最新版本,所以不推荐用各个 Linux 发行版的默认仓库进行安装,而是通过官方工具 certbot-auto 安装。
wget https://dl.eff.org/certbot-auto # 下载到本地
chmod a+x ./certbot-auto # 添加可执行权限
./certbot-auto --help all # 查看帮助
2. 验证域名所有权
配置 Nginx,使要获取证书的域名对应的 80 端口可以正常访问。在 /etc/nginx/conf.d/ 目录下为域名创建新文件 www.example.com.conf,并添加相关配置信息:
server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
重启 Nginx
systemctl restart nginx
3. 生成证书
certbot-auto --nginx -d kikakika.com -d www.kikakika.com
出现错误:
Error while running nginx -c /etc/nginx/nginx.conf -t.
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
解决方法:
/certbot-auto
--nginx --nginx-server-root=
/usr/local/nginx/conf
正常情况下,进入交互式界面,提示你输入邮箱(在证书失效前收到通知邮件),并同意官方协议。
[root@VM_120_242_centos tmp]# ./certbot-auto --nginx -d scall2.szhuizhong.cn
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for scall2.szhuizhong.cn
Waiting for verification...
Cleaning up challenges
Deployed Certificate to VirtualHost /etc/nginx/nginx.conf for scall2.szhuizhong.cn
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
No redirect - Make no further changes to the webserver configuration.
Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
证书生成成功后,会让你选择是否将所有的 HTTP 请求重定向到 HTTPS(输入 1 或者 2)。如果选 1,则通过 HTTP 和 HTTPS 都可以访问。如果选 2,则所有通过 HTTP 来的请求,都会被 301 重定向到 HTTPS。参考下面的 5. 配置 Nginx。
输完 1 或者 2 回车后,会有成功提示,并说明证书放在 /etc/letsencrypt/live/证书的域名 这个位置:
4. 开启 443 端口
开启443端口:firewall-cmd --permanent --add-port=443/tcp
查看是否成功:firewall-cmd --permanent --query-port=443/tcp
端口添加成功后,需要reload重新载入:firewall-cmd --reload
5. 配置 Nginx
certbot-auto 会自动调用 Nginx 的插件,改写配置文件。可以通过参数 certonly 关闭自动改写配置文件的功能,只获取证书。
./certbot-auto certonly -d kikakika.com
下面是 certbot-auto 自动改写过的配置文件,所有改写过的行都在后面加了注释:# managed by Certbot。
# 生成证书时选 1: No redirect,不把 HTTP 请求重定向到 HTTPS,一个 server 同时监听 80 和 443 端口
server {
listen 80;
server_name scall2.szhuizhong.cn;
root /home/szhuizhong/trunk;
location / {
if (-f $request_filename) {
expires max;
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
index index.html index.htm index.php l.php;
autoindex off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param CI_ENV 'testing';
include fastcgi_params;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/scall2.szhuizhong.cn/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/scall2.szhuizhong.cn/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
# 生成证书时选 2: redirect,HTTP 请求重定向到 HTTPS,两个 server 分别监听 80 和 443 端口
server {
root /home/kikakika/trunk;
server_name kikakika.com;
index index.html index.htm index.php;
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/kikakika.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/kikakika.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
server_name kikakika.com www.kikakika.com;
listen 80;
return 301 https://$host$request_uri; # managed by Certbot
}
6. 设置定时任务
出于安全策略, Let’s Encrypt 签发的证书有效期只有 90 天。通过 ./certbot-auto renew 命令可以续签。
- 编辑 crontab 文件:
$ crontab -e
添加 certbot 命令:
在每天凌晨3点运行。该命令将检查服务器上的证书是否将在未来30天内过期,如果是,则进行更新。--quiet 指令告诉 certbot 不要生成输出。
0 3 * * * /root/letsencrypt/certbot-auto renew --quiet
写入日志
0 0 1 * * /home/soft/ssl/letsencrypt/certbot-auto renew>>/home/soft/ssl/letsencrypt/log.txt
保存并关闭文件。所有安装的证书将自动更新并重新加载。
Nginx 实现 HTTPS(基于 Let's Encrypt 的免费证书)的更多相关文章
- 基于Let's Encrypt生成免费证书-支持多域名泛域名证书
目录 客户端 certbot acme.sh 安装acme.sh 1. 自动安装 2. 手动安装 3. 测试收否安装成功 使用acme.sh生成证书 1. HTTP 方式 2. DNS 方式 1. 生 ...
- nginx配置https及Android客户端访问自签名证书
前一篇随笔通过keytool生成keystore并为tomcat配置https,这篇随笔记录如何给nginx配置https.如果nginx已配置https,则tomcat就不需要再配置https了.通 ...
- Ubuntu Linux服务器搭建SSL/TLS(https)(在StartSSL可以得到免费证书)
目录 1 生成公钥和私钥对 2 公钥提交到CA机构签发一个crt证书 3 配置证书链 4 在Apache里开启SSL支持并配置crt证书和私钥 5 配置HSTS (可选) 6 总结 首先SSL/TLS ...
- Nginx 实现全站 HTTPS(基于 Let's Encrypt 的免费通配符证书)
单域名证书的生成可以 参考这里. acme.sh 项目中文文档 Let's Encrypt 在 18 年 1 月份推出了 ACME v2,支持通配符域名证书,对小网站.个人站长的友好度进一步增加. 常 ...
- nginx安装Lets Encrypt SSL免费HTTPS加密证书
Linux Nginx网站:Certbot安装配置Lets Encrypt SSL免费HTTPS加密证书 原文地址:https://renwole.com/archives/157 实验环境:Cent ...
- nginx+tomcat https实践
1. 安装ssl'证书 使用Let's Encrypt 的免费证书: 下载源代码: git clone https://github.com/letsencrypt/letsencrypt 我时阿里云 ...
- 免费证书https://lamp.sh/ssl.html
https(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的 http 通道,简单讲是 http 的安全版.即 ht ...
- 基于 Nginx 的 HTTPS 性能优化
前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...
- 基于 Nginx 的 HTTPS 性能优化实践
前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...
随机推荐
- HR问了我朋友什么是简单工厂模式,竟被质疑是否学过设计模式,是否是计算机专业?
越是简单的东西,越是容易被忽略,我来带你们好复习一下! 简单工厂模式 简单工厂模式也被称为静态工厂模式;使用简单工厂模式可以将产品的"消费"和生产完全分开,客户端只需要知道自己需要 ...
- Python(Redis 中 Set/Zset 类型数据和其他操作)
1.redis 基本操作命令 Set 操作 Set 集合就是不允许重复的列表 无序集合 sadd(name,values) 给 name 对应的集合中添加 1 个或多个元素 import redis ...
- DNS 处理模块 dnspython
简介: dnspython (http://www.dnspython.org/)是Python实现一个DNS的工具包,支持所有的记录类型,可以用于查询.传输并动态更新ZONE信息. 安装 wget ...
- Git 上传本地项目到远程仓库 (工具篇)
前言:前面一开始写了一篇通过命令来操作本地项目上传远程仓库的文章,后来发现此方式没有那么灵活.故跟开发同事请教了下,知道了通过工具来操作更方便.所以写了这篇文章来分享&记录. 前提条件:本地安 ...
- wmic 内网使用
先决条件: 1.远程服务器启动Windows Management Instrumentation服务,开放TCP135端口,防火墙放开对此端口的流量(默认放开): 2.远程服务器的本地安全策略的“网 ...
- 猫狗大战("简单的二维背包")
题面:https://www.luogu.com.cn/problem/P1489 看上去是一道简单的二维费用背包,但是要特别小心循环顺序. Ⅰ先循环物品,再循环限制条件. Ⅱ每一个限制条件都必须从后 ...
- 记一次jackson序列化Boolean的坑
@Data public class CouponTemplateDto { /** * 优惠券类型id */ private Long couponTypeId; /** * 优惠券模板id */ ...
- Spring cloud系列教程第二篇:支付项目父工程图文搭建
Spring cloud系列教程第二篇:支付项目父工程图文搭建 在讲解spring cloud相关的技术的时候,咱们就模拟订单支付这个流程来讲讲 在这个支付模块微服务搭建过程中,上面的这些技术,都会融 ...
- 推荐算法_CIKM-2019-AnalytiCup 冠军源码解读
最近在帮一初创app写推荐系统,顺便学习一波用户兴趣高速检索的冠军算法. 写总结前贴出冠军代码的git地址:https://github.com/ChuanyuXue/CIKM-2019-Analyt ...
- JPA与hibernate-------JPA
ORM概述 ORM(Object-Relational Mapping) 表示对象关系映射.在面向对象的软件开发中,通过ORM,就可以把对象映射到关系型数据库中.只要有一套程序能够做到建立对象与数据库 ...