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 502 Bad Gateway错误原因及解决方案
Mar 31 Servers
nginx proxy_cache 缓存配置详解
Mar 31 Servers
Nginx代理同域名前后端分离项目的完整步骤
Mar 31 Servers
Linux安装apache服务器的配置过程
Nov 27 Servers
Apache Linkis 中间件架构及快速安装步骤
Mar 16 Servers
Docker 镜像介绍以及commit相关操作
Apr 13 Servers
nginx配置之并发频次限制
Apr 18 Servers
Windows Server 2019 域控制器安装图文教程
Apr 28 Servers
解决Git推送错误non-fast-forward的方法
Jun 25 Servers
Docker安装MySql8并远程访问的实现
Jul 07 Servers
Win10系统搭建ftp文件服务器详细教程
Aug 05 Servers
码云(gitee)通过git自动同步到阿里云服务器
Dec 24 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
PHILIPS AE3805收音机的分析打磨
2021/03/02 无线电
探讨PHP中OO之静态关键字以及类常量的详解
2013/06/07 PHP
详解PHP字符串替换str_replace()函数四种用法
2017/10/13 PHP
laravel5实现微信第三方登录功能
2018/12/06 PHP
Display SQL Server Login Mode
2007/06/21 Javascript
jquery cookie插件代码类
2009/05/26 Javascript
最短的javascript:地址栏载入脚本代码
2011/10/13 Javascript
纯javascript实现的小游戏《Flappy Pig》实例
2015/07/27 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
vue实现登陆登出的实现示例
2017/09/15 Javascript
vue2实现数据请求显示loading图
2017/11/28 Javascript
基于dataset的使用和图片延时加载的实现方法
2017/12/11 Javascript
vue-cli脚手架引入弹出层layer插件的几种方法
2019/06/24 Javascript
微信小程序云开发如何实现数据库自动备份实现
2019/08/16 Javascript
javascript开发实现贪吃蛇游戏
2020/07/31 Javascript
通过实例解析JavaScript常用排序算法
2020/09/02 Javascript
利用vue3+ts实现管理后台(增删改查)
2020/10/30 Javascript
详解Python中使用base64模块来处理base64编码的方法
2016/07/01 Python
Python实现基本数据结构中队列的操作方法示例
2017/12/04 Python
python版学生管理系统
2018/01/10 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
Tornado Web Server框架编写简易Python服务器
2018/07/28 Python
windows下numpy下载与安装图文教程
2019/04/02 Python
Django的性能优化实现解析
2019/07/30 Python
CSS3实现线性渐变用法示例代码详解
2020/08/07 HTML / CSS
护理专业的自荐信
2013/10/22 职场文书
舞蹈兴趣小组活动总结
2014/07/07 职场文书
2014年政协委员工作总结
2014/12/01 职场文书
员工2014年度工作总结
2014/12/09 职场文书
文明医院的标语集锦!
2019/07/24 职场文书
五年级作文之成长
2019/09/16 职场文书
导游词之秦皇岛燕塞湖
2020/01/03 职场文书
用position:sticky完美解决小程序吸顶问题的实现方法
2021/04/24 HTML / CSS
Jupyter notebook 不自动弹出网页的解决方案
2021/05/21 Python
Dashboard管理Kubernetes集群与API访问配置
2022/04/01 Servers
如何利用python创作字符画
2022/06/25 Python