Nginx配置https原理及实现过程详解


Posted in Servers onMarch 31, 2021

使用linux实用工具certbot来生成https证书

这个工具是生成Let's Encrypt证书,

Let's Encrypt数字证书认证机构,Let's Encrypt 是由互联网安全研究小组(ISRG,一个公益组织)提供的服务

提供免费的SSL/TLS证书

2015年12月3日,该服务进入公测阶段,正式面向公众。

2016年4月12日,该项目正式离开Beta阶段。

到2016年9月9日,Let's Encrypt 已经发放 1000 万张证书。

因此对于大部分中小型网站来说,是一个值得考虑的选择。

https配置的步骤

1打开 https://certbot.eff.org/ 选择对应操作系统与 Web 服务器

这里我选择nginx服务器,CentOS7服务器上

2执行命令,并根据需要修改相应域名参数。

certbot要通过yum安装,certbot被打包到epel源中,

所以安装启动epel库,安装epel源查看链接

https://fedoraproject.org/wiki/EPEL#How_can_I_use_these_extra_packages.3F

启动epel源,可以使用手动自己启动epel,也可以借助yum-config-manager命令来启动

安装yum-config-manager

yum -y install yum-utils

启动epel

yum-config-manager --enable rhui-REGION-rhel-server-extras rhui-REGION-rhel-server-optional

3安装certbot

sudo yum install certbot python2-certbot-nginx

获取证书的两种方式:身份验证器和安装程序

使用webRoot插件进行安装,这个要求你的服务器80端口能够正常被访问到(这个域名是属于你的)

webRoot插件通过certonly和--webroot(或者-w)在命令行上执行命令

certbot certonly -w /var/www/example -d www.example.com

certbot certonly -w 可以被http访问到的webroot目录 -d 要配置https的域名名称

上面的 /var/www/example表示的是在nginx配置文件中root根节点所指向的根路径

webroot插件的工作原理是为每个请求的域创建一个临时文件${webroot-path}/.well-known/acme-challenge。

然后,Let的加密验证服务器发出HTTP请求,以验证每个请求的域的DNS是否解析为运行certbot的服务器。

访问请求如下

66.133.109.36 - - [05/Jan/2016:20:11:24 -0500] "GET /.well-known/acme-challenge/HGr8U1IeTW4kY_Z6UIyaakzOkyQgPr_7ArlLgtZE8SX HTTP/1.1" 200 87 "-" "Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)"

所以我们服务器需要放通.well-known/acme-challenge这个访问路径

例如,

server
  {
    listen 80;
    server_name www.example.com; 
    index index.html ;
    root /var/www/example;
  
    。。。
  
    location ~ /.well-known {
      allow all;
    }
  }

具体的http配置文件

server
  {
    listen 80;
    server_name www.example.com; 
    index index.html ;
    root /var/www/www.example.com;


    location / {
      proxy_redirect off;
      proxy_pass http://localhost:8080;
      proxy_set_header Host $host;
      proxy_set_header  X-real-ip $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #error_page  404  /404.html;

    location /nginx_status
    {
      #stub_status on;
      #access_log  off;
    }

    location ~ /.well-known {
      allow all;
    }

    location ~ /\.
    {
      deny all;
    }
access_log /data/log/nginx//var/www/www.example.com/-access.log;
    error_log /data/log/nginx//var/www/www.example.com/-error.log;
}

执行完命令后,https证书就会生成在/etc/letsencrypt/live目录下

certbot certonly -w /var/www/example -d www.example.com

比如上面的命令会生成证书/etc/letsencrypt/live/www.example.com/fullchain.pem

生成证书密钥文件/etc/letsencrypt/live/www.example.com/privkey.pem

然后我们只需要为该域名加上https配置,我们nginx就配置完成https

https对应443端口

具体https配置文件

server
  {
    listen 443 ssl http2;
    #listen [::]:443 ssl http2;
    server_name www.example.com;
    index index.html index.htm index.php default.html default.htm default.php;
    root /var/www/www.example.com/;
    
    ssl on;
    ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
    
   location / {
      proxy_redirect off;
      proxy_pass http://localhost:8080;
      proxy_set_header Host $host;
      proxy_set_header  X-real-ip $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    #error_page  404  /404.html;

    include enable-php-pathinfo.conf;

    location ~ /.well-known {
      allow all;
    }

    location ~ /\.
    {
      deny all;
    }

    access_log /data/log/nginx/www.example.com-ssl-access.log;
    error_log /data/log/nginx/www.example.com-ssl-error.logs;  
}

查看生产的证书

tree /etc/letsencrypt/live/

证书续签

Let's Encrypt 生成的免费证书为3个月时间,但是我们可以无限次续签证书

certbot renew

使用定时器来自动重新生成证书

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew

centos6使用

1获取certbot客户端

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

2停止nginx

service nginx stop

3生成证书

./certbot-auto certonly --standalone --email `你的邮箱地址` -d `你的域名地址`

当前网站有多个域名时需在后面增加,例如

./certbot-auto certonly --standalone --email `你的邮箱地址` -d `你的域名1` -d `你的域名2`

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Servers 相关文章推荐
Nginx配置并兼容HTTP实现代码解析
Mar 31 Servers
Nginx反爬虫策略,防止UA抓取网站
Mar 31 Servers
Nginx实现会话保持的两种方式
Mar 18 Servers
解决xampp安装后Apache无法启动
Mar 21 Servers
Windows Server 2016 配置 IIS 的详细步骤
Apr 28 Servers
Windows Server 2008 修改远程登录端口以及配置防火墙
Apr 28 Servers
nginx实现多geoserver服务的负载均衡
May 15 Servers
服务器间如何实现文件共享
May 20 Servers
netty 实现tomcat的示例代码
Jun 05 Servers
win server2012 r2服务器共享文件夹如何设置
Jun 21 Servers
Nginx报404错误的详细解决方法
Jul 23 Servers
Nginx如何配置根据路径转发详解
Jul 23 Servers
如何在centos上使用yum安装rabbitmq-server
Mar 31 #Servers
Windows下使用Nginx+Tomcat做负载均衡的完整步骤
阿里云Nginx配置https实现域名访问项目(图文教程)
详解Nginx 工作原理
fastdfs+nginx集群搭建的实现
Nginx域名转发https访问的实现
Mar 31 #Servers
Nginx本地目录映射实现代码实例
Mar 31 #Servers
You might like
Linux下进行MYSQL编程时插入中文乱码的解决方案
2007/03/15 PHP
PHP生成带有雪花背景的验证码
2008/09/28 PHP
PHP获取服务器端信息的方法
2014/11/28 PHP
php判断手机浏览还是web浏览,并执行相应的动作简单实例
2016/07/28 PHP
A标签触发onclick事件而不跳转的多种解决方法
2013/06/27 Javascript
javascript实现按回车键切换焦点
2015/02/09 Javascript
JavaScript数组去重的两种方法推荐
2016/04/05 Javascript
jQueryUI DatePicker 添加时分秒
2016/06/04 Javascript
用JS中split方法实现彩色文字背景效果实例
2016/08/24 Javascript
微信JS-SDK自定义分享功能实例详解【分享给朋友/分享到朋友圈】
2016/11/25 Javascript
js获取隐藏元素的宽高
2017/02/24 Javascript
关于vue.js过渡css类名的理解(推荐)
2017/04/10 Javascript
vue+vuex+axios实现登录、注册页权限拦截
2018/03/09 Javascript
微信小程序实现倒计时调用相机自动拍照功能
2018/06/10 Javascript
解决vue.js this.$router.push无效的问题
2018/09/03 Javascript
nodejs搭建本地服务器并访问文件操作示例
2019/05/11 NodeJs
Vue+ElementUI项目使用webpack输出MPA的方法
2019/08/27 Javascript
[00:05]ChinaJoy现场 DOTA2玩家高呼“CN DOTA BEST DOTA”
2019/08/04 DOTA
python静态方法实例
2015/01/14 Python
python实现简单的计时器功能函数
2015/03/14 Python
python分割列表(list)的方法示例
2017/05/07 Python
python使用fcntl模块实现程序加锁功能示例
2017/06/23 Python
Python字符编码与函数的基本使用方法
2017/09/30 Python
从CentOS安装完成到生成词云python的实例
2017/12/01 Python
Python决策树和随机森林算法实例详解
2018/01/30 Python
django框架模板语言使用方法详解
2019/07/18 Python
使用BeautifulSoup4解析XML的方法小结
2020/12/07 Python
css3 2D图片转动样式可以扩充到Js当中
2014/04/29 HTML / CSS
HTML5实现多张图片上传功能
2016/03/11 HTML / CSS
Prototype是怎么扩展DOM的
2014/10/01 面试题
2014党员干部四风问题对照检查材料思想汇报
2014/09/24 职场文书
2014年助理工程师工作总结
2014/11/14 职场文书
签订劳动合同通知书
2015/04/16 职场文书
无房证明样本
2015/06/17 职场文书
2015年机关作风和效能建设工作总结
2015/07/23 职场文书
微信小程序纯CSS实现无限弹幕滚动效果
2022/09/23 HTML / CSS