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 反向代理之 proxy_pass的实现
Mar 31 Servers
阿里云Nginx配置https实现域名访问项目(图文教程)
Mar 31 Servers
nginx+lua单机上万并发的实现
May 31 Servers
nginx反向代理配置去除前缀案例教程
Jul 26 Servers
Windows server 2012 R2 安装IIS服务器
Apr 29 Servers
利用nginx搭建RTMP视频点播、直播、HLS服务器
May 25 Servers
使用Apache Camel表达REST服务的方法
Jun 10 Servers
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
Jun 10 Servers
彻底卸载VMware虚拟机的超详细步骤记录
Jul 15 Servers
在windows server 2012 r2中安装mysql的详细步骤
Jul 23 Servers
nginx访问报403错误的几种情况详解
Jul 23 Servers
Valheim服务器 Mod修改安装教程 【ValheimPlus】
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
iOS+PHP注册登录系统 PHP部分(上)
2016/12/26 PHP
php慢查询日志和错误日志使用详解
2021/02/27 PHP
javascript循环变量注册dom事件 之强大的闭包
2010/09/08 Javascript
js操纵跨frame的三级联动select下拉选项实例介绍
2013/05/19 Javascript
15款jQuery分布引导插件分享
2015/02/04 Javascript
简介JavaScript中getUTCMonth()方法的使用
2015/06/10 Javascript
JavaScript对象数组排序函数及六个用法
2015/12/23 Javascript
简介BootStrap model弹出框的使用
2016/04/27 Javascript
微信小程序 时间格式化(util.formatTime(new Date))详解
2016/11/16 Javascript
vue服务端渲染的实例代码
2017/08/28 Javascript
Angular实现较为复杂的表格过滤,删除功能示例
2017/12/23 Javascript
[01:07]2015国际邀请赛 中国区预选赛精彩回顾
2015/06/15 DOTA
探究Python多进程编程下线程之间变量的共享问题
2015/05/05 Python
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
2015/05/20 Python
Python读大数据txt
2016/03/28 Python
python os.path模块常用方法实例详解
2018/09/16 Python
python使用wxpy轻松实现微信防撤回的方法
2019/02/21 Python
python2和python3在处理字符串上的区别详解
2019/05/29 Python
Python命令行click参数用法解析
2019/12/19 Python
通过实例解析python描述符原理作用
2020/01/22 Python
Python greenlet和gevent使用代码示例解析
2020/04/01 Python
详解python对象之间的交互
2020/09/29 Python
selenium判断元素是否存在的两种方法小结
2020/12/07 Python
canvas像素点操作之视频绿幕抠图
2018/09/11 HTML / CSS
印尼在线精品店:Berrybenka.com
2016/10/22 全球购物
元旦晚会邀请函
2014/02/01 职场文书
运动会稿件200字
2014/02/07 职场文书
销售职业生涯规划范文
2014/03/14 职场文书
转让协议书范本
2014/04/15 职场文书
法制宣传标语
2014/06/23 职场文书
村长党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
王兆力在市委党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
2016年党员公开承诺书范文
2016/03/24 职场文书
导游词之江苏同里古镇
2019/11/18 职场文书
PHP策略模式写法
2021/04/01 PHP
利用Python脚本写端口扫描器socket,python-nmap
2022/07/23 Python