WEB服务-Nginx之09-https

WEB服务-Nginx之09-https

HTTPS安全证书基本概述

为什么需要使用HTTPS?

因为HTTP不安全,当我们使用HTTP网站时,容易遭到劫持和篡改,如果采用HTTPS协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。

HTTPS=HTTP+SSL/TLS

TLS是SSL的新版本,现在常用。


TLS介绍

在OSI七层模型中,应用层是HTTP协议,在应用层协议之下的表示层,是SSL协议发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,在应用层http协议没有感知的情况下做到了数据的安全加密。

5e6c4d9595017321ad000003

在数据进行加密与解密过程!中,需要有一个权威机构来验证双方身份,这个权威机构就是CA机构。


CA机构颁发证书及验证流程

5e6c4daf95017321ad000004

  • 首先需要申请证书,先去登记机构进行身份登记,我是谁,我是干嘛的,我想做什么,
  • 然后登记机构再通过CSR发给CA,
  • CA中心通过后会生成一堆公钥和私钥,公钥会在CA证书链中保存,公钥和私钥证书给我们,
  • 最后我们将公钥和私钥证书部署在WEB服务器上
  1. 浏览器访问https站点,HTTP连接建立成功后,浏览器首先请求web服务器的证书

  2. web服务器会将其上的公钥证书发给浏览器

  3. 浏览器去CA机构提供的站点验证证书是否合法有效

CA机构会将过期的证书放置在CRL服务器,但是CRL服务的验证效率非常差,所以CA又推出了OCSP响应程序。

OCSP响应程序可以查询指定的一个证书是否过期,所以浏览器可以直接查询OSCP响应程序,但OSCP响应程序的性能还不是很高。

Nginx有一个OCSP的开关,开启后,Nginx会主动去OCSP上查询并缓存,这样客户端可以直接从Nginx获取证书是否有效。


浏览器访问流程

  1. 浏览器往服务器的443端口发起请求,请求携带了浏览器支持的加密算法和哈希算法。

  2. 服务器收到请求,选择浏览器支持的加密算法和哈希算法。

  3. 服务器将数字证书返回给浏览器,这里的数字证书可以是向某个可靠机构申请的,也可以是自制的。

  4. 浏览器进入数字证书认证环节,这一部分是在浏览器内置的TLS中完成的:

4.1 首先浏览器会从内置的证书列表中索引,找到服务器下发证书对应的机构,如果没有找到,此时就会提示用户该证书是不是由权威机构颁发,是不可信任的。如果查到了对应的机构,则取出该机构颁发的公钥。

4.2 用机构的证书公钥解密得到证书的内容和证书签名,内容包括网站的网址、网站的公钥、证书的有效期等。浏览器会先验证证书签名的合法性(验证过程类似上面Bob和Susan的通信)。签名通过后,浏览器验证证书记录的网址是否和当前网址是一致的,不一致会提示用户。如果网址一致会检查证书有效期,证书过期了也会提示用户。这些都通过认证时,浏览器就可以安全使用证书中的网站公钥了。

4.3 浏览器生成一个随机数R,并使用网站公钥对R进行加密。

  1. 浏览器将加密的R传送给服务器。

  2. 服务器用自己的私钥解密得到R。

  3. 服务器以R为密钥使用对称加密算法加密网页内容并传输给浏览器。

  4. 浏览器以R为密钥使用之前约定好的解密算法解密收到的网页内容。

模拟网站被篡改

  1. web01服务器nginx配置并重载
[root@web01 ~]# cat > /etc/nginx/conf.d/rewrite.conf <<EOF
server {
    listen 80;
    server_name www.rewrite.com;
    root /code;
    index index.html;
    charset utf-8;
}
EOF
[root@web01 ~]# systemctl reload nginx

  1. 创建所需页面
[root@web01 ~]# cat > /code/index.html <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我是title</title>
</head>
<body>
<article>
<header>
    <h1>美好的一天从现在开始</h1>
    <p>创建时间:<time pubdate="pubdate">2020/5/20</time></p>
</header>
    <p>
    <b>Aticle</b>第一次用h5写文章,好紧张...
    </p>
    <footer>
    <p><small>版权所有!</small></p>
    </footer>
</article>
</body>
</html>
EOF

  1. 浏览器访问页面www.rewrite.com

1600915643103


  1. lb01服务器nginx拦截(代理)配置并重载
[root@lb01 ~]# cat > /etc/nginx/conf.d/jiechi.conf <<EOF
upstream jiechi {
    server 10.0.0.7:80;
}

server {
   listen 80;
   server_name www.rewrite.com;

   location / {
       proxy_pass http://jiechi;
       proxy_set_header Host \$http_host;
       sub_filter '<h1>美好的一天从现在开始' '<h1>最后的日子';
       sub_filter '<small>版权所有' '<small>开源';
   }
}
EOF
[root@lb01 ~]# systemctl reload nginx

  1. windows配置:

C:\Windows\System32\drivers\etc\hosts文件中修改10.0.0.5 www.rewrite.com

  1. 浏览器访问页面www.rewrite.com

1600917167281


证书类型介绍

对比 域名型 DV 企业型 OV 增强型 EV
绿色地址栏 5e6c4e0595017321ad000005 小锁标记+https 小锁标记+h5e6c4e3c95017321ad000006ttps 5e6c4e5595017321ad000007小锁标记+企业名称+https
一般用途 个人站点和应用; 简单的https加密需求 电子商务站点和应用; 中小型企业站点 大型金融平台; 大型企业和政府机构站点
审核内容 域名所有权验证 全面的企业身份验证; 域名所有权验证 最高等级的企业身份验证; 域名所有权验证
颁发时长 10分钟-24小时 3-5个工作日 5-7个工作日
单次申请年限 1年 1-2年 1-2年
赔付保障金 —— 125-175万美金 150-175万美金

证书购买选择

保护一个域名 www

保护五个域名 www images cdn test m

通配符域名 *.oldboy.com


HTTPS注意事项

https不支持续费,证书到期需要重新申请并进行替换
https不支持三级域名解析,如 test.m.oldboy.com
https显示绿色,说明整个网站的url都是https的
https显示黄色,因为网站代码中包含http的不安全链接
https显示红色,那么证书是假的或者证书过期。

Nginx单台实现HTTPS

  1. nginx必须有ngx_http_ssl_module模块,官方源yum安装默认有
[root@web01 ~]#  nginx -V 2>&1 | tr " " "\n"| grep http_ssl_module
--with-http_ssl_module
  1. 创建存放ssl证书的目录
[root@web01 ~]# mkdir -p /etc/nginx/ssl_key
  1. 使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,这是不被互联网认可的黑户证书)

    生成私钥,自签证书

[root@web01 ~]# openssl req -days 3650 -x509 -sha256 -nodes -newkey rsa:2048 -keyout /etc/nginx/ssl_key/server.key -out /etc/nginx/ssl_key/server.crt
Generating a 2048 bit RSA private key
.+++
...................................+++
writing new private key to '/etc/nginx/ssl_key/server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:China
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:CHINA
Locality Name (eg, city) [Default City]:Name
Organization Name (eg, company) [Default Company Ltd]:Test
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:web01
Email Address []:123@qq.com

# req      --> 用于创建新的证书
# days     --> 证书的有效期
# x509     --> 定义证书的格式为标准格式,专用于CA生成自签证书
# sha256   --> 加密算法
# nodes    --> 不输出REQ
# newkey   --> 表示生成新的私钥文件
# rsa:2048 --> 加密算法
# keyout   --> 私钥文件输出路径
# out      --> 证书文件输出路径
  1. 配置证书
[root@web01 ~]# vi /etc/nginx/conf.d/ssl.conf 
server {
    listen 443 ssl;
    server_name s.ssl.com;
    ssl_certificate   ssl_key/server.crt;
    ssl_certificate_key  ssl_key/server.key;
    location / {
        root /code;
        index index.html;
    }
}
#配置将用户访问http请求强制跳转https
server {
    listen 80;
    server_name s.ssl.com;
    return 302 https://$server_name$request_uri;
}
  1. 准备对应的站点目录,重载Nginx
[root@web01 ~]# echo "https" > /code/index.html
[root@web01 ~]# systemctl reload nginx
  1. windows配置:

C:\Windows\System32\drivers\etc\hosts文件中添加一行10.0.0.7 s.ssl.com

  1. 浏览器访问测试

1600936711660

5e6c4c6695017321ad000002

1600936940736


参数介绍

# 启动ssl功能,该指令在版本1.15.0中已作废,应改用listen指令的ssl参数。
Syntax:  ssl on | off;
Default: ssl off;
Context: http,server

# 指定证书文件
Syntax:  ssl_certificate file;
Default: -
Context: http,server

# 指定私钥文件
Syntax:  ssl_certificate_key fil;
Default: -
Context: http,server

Nginx集群实现HTTPS

Nginx负载均衡 + Nginx WEB配置HTTPS

5e6c926a95017321ad000009


环境准备

主机名 外网IP(NAT) 内网IP(LAN) 角色
lb01 10.0.0.5 172.16.1.5 负载均衡
web01 10.0.0.7 172.16.1.7 web服务器
web02 10.0.0.8 172.16.1.8 web服务器

web01、web02服务器配置相同

[root@web01 ~]# cat > /etc/nginx/conf.d/ssl.conf <<EOF
server {
        listen 80;
        server_name s.ssl.com;

        location / {
                root /code;
                index index.html;
        }
}
EOF
[root@web01 ~]# echo "web01_https" > /code/index.html

[root@web02 ~]# cat > /etc/nginx/conf.d/ssl.conf <<EOF
server {
        listen 80;
        server_name s.ssl.com;

        location / {
                root /code;
                index index.html;
        }
}
EOF
[root@web02 ~]# echo "web02_https" > /code/index.html

拷贝web01生成的证书到lb01

[root@lb01 ~]# scp -rp root@172.16.1.7:/etc/nginx/ssl_key /etc/nginx/

lb01配置

[root@lb01 ~]# vi /etc/nginx/conf.d/proxy_ssl.conf 
upstream website {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
    listen 443 ssl;
    server_name s.ssl.com;
    ssl_certificate ssl_key/server.crt;
    ssl_certificate_key ssl_key/server.key;
    location / {
        proxy_pass http://website;
        proxy_set_header Host $http_host;
    }
}

server {
    listen 80;
    server_name s.ssl.com;
    return 302 https://$server_name$request_uri;
}:

三台服务器重载nginx

[root@lb01 ~]# systemctl reload nginx
[root@web01 ~]# systemctl reload nginx
[root@web02 ~]# systemctl reload nginx

windows配置:

C:\Windows\System32\drivers\etc\hosts文件中修改10.0.0.5 s.ssl.com

浏览器访问测试

1600939108495

1600939132766

业务场景实现HTTPS

lb01配置博客、知乎、phpmyadmin并重载

[root@lb01 ~]# vi /etc/nginx/conf.d/proxy_web.conf 
upstream web {
        server 172.16.1.7:80;
        server 172.16.1.8:80;
}

server {
        listen 80;
        server_name blog.oldboy.com;
        return 302 https://$server_name$request_uri;
}

server {
        listen 80;
        server_name zh.oldboy.com;
        return 302 https://$server_name$request_uri;
}

server {
        listen 80;
        server_name php.oldboy.com;
        return 302 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        server_name blog.oldboy.com;
        ssl_certificate ssl_key/server.crt;
        ssl_certificate_key ssl_key/server.key;
        location / {
                proxy_pass http://web;
                include proxy_params;
        }
}

server {
        listen 443 ssl;
        server_name zh.oldboy.com;
        ssl_certificate ssl_key/server.crt;
        ssl_certificate_key ssl_key/server.key;
        location / {
                proxy_pass http://web;
                include proxy_params;
        }       
}

server {
        listen 443 ssl;
        server_name php.oldboy.com;
        ssl_certificate ssl_key/server.crt;
        ssl_certificate_key ssl_key/server.key;

        location / {
            proxy_pass http://web;
            include proxy_params;
        }
}

[root@lb01 conf.d]# nginx -s reload

windows配置:

C:\Windows\System32\drivers\etc\hosts文件中修改10.0.0.5 blog.oldboy.com zh.oldboy.com php.oldboy.com


打开浏览器访问blog.oldboy.com,发现页面格式不对

1600939480583

wordpress早期安装如果是使用的http方式,那开启https后会导致图片出现破损或加载不全的情况

建议:
1、在安装WordPress之前就配置好https;
2、在WordPress后台管理页面,设置-->常规-->修改(WordPress地址及站点地址)为 https://(现在不用)
3、注意:WordPress很多链接在安装时被写入数据库中。


打开浏览器访问php.oldboy.com,发现警告HTTPS不匹配

5e6ca0c895017321ad00000d


修改配置博客、知乎、phpmyadmin的后端web配置并重载(web01和web02配置相同)

问题原因:负载访问使用https协议,后端web使用http协议,对于PHP来说他并不知道用的到底是什么,所以会出现错误。

[root@web01 ~]# vi /etc/nginx/conf.d/wordpress.conf 
server {
    listen 80;
    server_name blog.oldboy.com;
    root /code/wordpress;
    index index.php index.html;
    client_max_body_size 100m;

    location ~ \.php$ {
        root /code/wordpress;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # 告诉PHP前置的负载使用的是https协议
        fastcgi_param  HTTPS on;
        include        fastcgi_params;
    }
}

[root@web01 ~]# vi /etc/nginx/conf.d/zh.conf 
server {
    listen 80;
    server_name zh.oldboy.com;
    root /code/wecenter;
    index index.php index.html;

    location ~ \.php$ {
        root /code/wecenter;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param  HTTPS on;
        include        fastcgi_params;
    }
}

[root@web01 ~]# vi /etc/nginx/conf.d/php.conf 
server {
    listen 80;
    server_name php.oldboy.com;
    root /code/phpMyAdmin-4.8.4-all-languages;

    location / {
        index index.php index.html;
    }

    location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
        include       fastcgi_params;
    }
}

[root@web01 ~]# systemctl reload nginx
[root@web02 ~]# systemctl reload nginx

5e6ca0e295017321ad00000e

阿里云配置https[扩展]

按量付费购买两台实例

image-20200926212251107

image-20200926212353195

image-20200926212421801

image-20200926212447427


image-20200926212512204

购买SLB实例

image-20200926212535413

image-20200926212554022


配置负载均衡实例22端口转发

image-20200926212621600

image-20200926212729967


image-20200926212751765

image-20200926212815565

image-20200926212835284

配置负载均衡实例80端口转发

image-20200926212921630

image-20200926212951666

image-20200926213010743

image-20200926213030428

配置服务器

利用端口转发连接没有外网的服务器实例(两台机器)

image-20200926213050979


修改主机名并安装nginx(两台机器)

[root@iZ8vbh829ac9d80gcle4ihZ ~]# hostnamectl set-hostname test01
[root@iZ8vbh829ac9d80gcle4ihZ ~]# bash
[root@test01 ~]# yum -y install nginx


配置并启动nginx(两台机器)

[root@test01 ~]# vim /etc/nginx/nginx.conf
server {
      listen       80;
      server_name  test.linuxnc.com;
      root         /usr/share/nginx/html;
}
[root@test01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@test01 ~]# nginx

配置页面

[root@test01 ~]# echo 111111111111 > /usr/share/nginx/html/index.html
[root@test02 ~]# echo 222222222222 > /usr/share/nginx/html/index.html

购买并配置证书

image-20200926213112380


证书申请绑定、验证、审核

image-20200926213136637

image-20200926213159806

image-20200926213229102


image-20200926213309110

部署证书

image-20200926213336268


image-20200926213402788

SLB配置证书访问

image-20200926213435164

image-20200926213459295

image-20200926213532022

image-20200926213559830

image-20200926213620972

image-20200926213641557

ssl优化参数

server {
    listen 443 default_server;
    server_name test.linuxnc.com;
    ssl on;
    root /var/www/wordpress;
    index index.php index.html index.htm;
    ssl_certificate   ssl/215089466160853.pem;
    ssl_certificate_key  ssl/215089466160853.key;
    # ssl优化参数
    ssl_session_cache shared:SSL:10m; # 在建立完ssl握手后如果断开连接,在session_timeout时间内再次连接,是不需要再次获取公钥建立握手的,可以复用之前的连接
    ssl_session_timeout 1440m;  # ssl连接断开后的超时时间
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  # 配置加密协议
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  # 使用TLS版本协议
    ssl_prefer_server_ciphers on;  # nginx决定使用哪些协议与浏览器通信
posted @ 2021-03-07 22:40  上善若水~小辉  阅读(301)  评论(0编辑  收藏  举报