• 升级Https前的可行性验证(一)


    升级Https之前的可行性验证

    注意:自签证书和Nginx的安装都基于ContOS 6

    一、如何申请OpenSSL自签证书

    1、安装OpenSSL

    (一)OpenSSL 工具下载

    下载地址

    (二)OpenSSL 安装

    参考博客

    • 查看服务器是否安装有OpenSSL
      openssl version -a
      • 1
    • 将下载的OpenSSL源码上传至Linux服务器

      可以使用Xshell的Xftp工具。

    • 解压上传的.tar.gz压缩包
      tar -zxvf openssl-1.1.1-pre8.tar.gz
      • 1
    • 安装gcc编译器,如果已经安装请略过
      
      #如果系统安装了gcc编译器,如下图所示的gcc version
      
      gcc -v 
      
      
      #安装gcc
      
      yum install gcc-c++
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

    这里写图片描述

    • 安装zlib库,如果已经安装请略过
      
      #检查是否安装zlib,如果安装如下图所示
      
      whereis zlib
      
      
      #获取zlib源码包
      
      wget http://zlib.net/zlib-1.2.11.tar.gz
      
      
      #切换到zlib源码包中
      
      cd zlib-1.2.11
      
      
      #安装zlib
      
      ./configure && make && make install
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19

    这里写图片描述

    • 安装OpenSSL工具
      
      #首先进入OpenSSL工具解压之后的目录
      
      cd openssl-1.1.1-pre8
      
      
      #--prefix=指定的安装路径
      
      ./config shared zlib  --prefix=/usr/local/openssl && make && make install
      
      
      #安装完成之后再当前目录再执行下面命令
      
      ./config -t make depend
      
      
      #然后进入OpenSSL的安装目录
      
      cd /usr/local
      
      
      #建立文件链接
      
      ln -s openssl ssl
      
      
      #打开etc下的这个ld.so.conf配置文件,然后再文本中添加/usr/local/openssl/lib
      
      vim /etc/ld.so.conf
      
      
      #执行命令使文件链接共享生效
      
      ldconfig
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
    • 环境变量配置
      
      #打开etc目录下的profile文件
      
      vim /etc/profile
      
      
      #然后再文件的末尾添加如下内容
      
      export OPENSSL=/usr/local/openssl/bin
      export PATH=$OPENSSL:$PATH:$HOME/bin
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    • 重开命令窗口,加载环境变量

    2、自签证书

    参考博客

    (一)生成根证书

    • 生成.key后缀私钥
      
      #生成私钥到指定目录
      
      openssl genrsa -out /usr/local/nginx/conf/rootca.key
      • 1
      • 2
      • 3
      • 4
    • 生成.csr后缀的证书申请文件
      
      #通过私钥生成申请文件到指定目录
      
      openssl req -new -key /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/rootca.csr
      • 1
      • 2
      • 3
      • 4
    • 生成.crt后缀的证书文件
      
      #通过私钥和证书申请文件,来自签证书
      
      openssl x509 -req -days 3650 -in /usr/local/nginx/conf/rootca.csr -signkey /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/rootca.crt
      • 1
      • 2
      • 3
      • 4

    (二)通过根证书签发服务端证书

    • 生成.key后缀私钥
      
      #生成服务端私钥
      
      openssl genrsa -out /usr/local/nginx/conf/server.key
      • 1
      • 2
      • 3
      • 4
    • 生成.csr后缀的证书申请文件
      
      #生成服务端证书申请文件
      
      openssl req -new -key /usr/local/nginx/conf/server.key -out /usr/local/nginx/conf/server.csr
      • 1
      • 2
      • 3
      • 4
    • 生成.crt后缀的证书文件
      
      #签发服务端证书文件
      
      openssl ca -in /usr/local/nginx/conf/server.csr -cert /usr/local/nginx/conf/rootca.crt -keyfile /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/server.csr
      • 1
      • 2
      • 3
      • 4

    二、Nginx 安装

    1、安装Nginx

    参考博客

    (一)下载相关源码包

    • 下载Nginx源码包
      
      #通过wget命令来远程获取源码包到当前目录
      
      wget http://nginx.org/download/nginx-1.15.2.tar.gz ./
      • 1
      • 2
      • 3
      • 4
    • 下载Pcre源码包
      
      #通过wget命令来远程获取源码包到当前目录
      
      wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz ./
      • 1
      • 2
      • 3
      • 4

    (二)安装

    • 安装Pcre
      
      #解压压缩包
      
      tar -zxvf pcre-8.40.tar.gz
      
      
      #切换到解压的目录中
      
      cd ./pcre-8.40
      
      
      #安装
      
      ./configure && make && make install
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 安装Nginx
      
      #解压压缩包
      
      tar -zxvf nginx-1.15.2.tar.gz
      
      
      #切换到解压的目录中
      
      cd ./nginx-1.15.2
      
      
      #安装
      
      ./configure && make && make install
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    2、安装Https模块

    参考博客

    • 如果之前有安装过Nginx并且配置过nginx.conf,那么一定先做备份
      
      #备份配置文件,前面是文件名,后面携带备份时间
      
      cp nginx.conf ./nginx.conf.2018816
      
      
      #备份安装目录sbin中 nginx运行文件
      
      cp ./nginx/sbin/nginx ./nginx/sbin/nginx2018816
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    • 在源码目录下安装Https模块
      
      #备份安装目录sbin中 nginx运行文件
      
      cp ./nginx/sbin/nginx ./nginx/sbin/nginx2018816
      
      
      #先cd到源码包中
      
      
      #获取https模块到指定目录
      
      ./configure --prefix=./nginx --with-http_stub_status_module --with-http_ssl_module
      
      
      #编译
      
      make
      
      
      #将编译好的nginx运行文件复制到安装目录的sbin中
      
      cp ./objs/nginx /usr/local/nginx/sbin/
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
    • 验证Https模块是否安装成功
      
      #通过命令查看Https模块是否安装成功,如果安装成功如下图所示
      
      /usr/local/nginx/sbin/nginx -V
      • 1
      • 2
      • 3
      • 4

    这里写图片描述

    3、启动Nginx验证是否安装成功

    • Nginx 常用的一些命令
      
      #测试nginx.conf文件是否配置正确
      
      ./sbin/nginx -t
      
      
      #启动nginx
      
      ./sbin/nginx
      
      
      #重启
      
      ./sbin/nginx -s reload
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 启动Nginx

      访问Nginx部署的服务器ip地址,出现如下图所示页面则表示安装成功。

    这里写图片描述

    三、Nginx 配置

    1、Nginx 配置Https

    (一)配置端口监听

    listen       443 ssl;
    server_name  10.3.1.2;
    • 1
    • 2

    (二)配置证书

    ssl_certificate      /usr/local/nginx/conf/server.crt;
    ssl_certificate_key  /usr/local/openssl/bin/nopass-server.key;
    • 1
    • 2

    (三)其他参数配置

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    
    server_tokens off;
    
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、Nginx 配置请求重定向和代理

    <!-- 配置重定向 -->
    location = /xxx {
        return 302 http://10.3.1.2:18080/bms_core;
    }
    
    <!-- 配置代理 -->
    location = /bms_core/ {
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.3.1.1:18080;
    }
    
    <!-- 
        注意:
            1、location后接的是请求匹配规则
            2、在代理时如果,转发的地址最后加了/,那么location后匹配的请求路径不会被代理到proxy_pass指定的路径后,如果不加/,那么location后的匹配路径就会添加在proxy_pass指定的代理路径后。示例:如果加/就会是这样http://10.3.1.1:18080,如果不加/就会使这样http://10.3.1.1:18080/bms_core/
            3、在代理配置时加上proxy_set_header Host $host:$server_port;的作用就是在代理时端口号就不会被去掉
    -->
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3、Nginx 配置项目静态资源

    <!-- 静态资源配置
        注意:如果监听了80端口(Http)和445端口(https),如果不配置静态资源的代理,则会出现静态资源无法访问的情况。原因是因为,如果是一个Html页面,你在第一次访问的时候给你返回之后,在Html中引用的一个静态资源是重新发起请求去获取的,当请求到了80端口和445端口就会再次取来匹配location,因为这个时候没有配置就会出现静态资源无法访问。
        解决方案:1、配置静态资源代理
                2、将Nginx服务器作为静态资源访问服务器,然后再配置如果请求静态资源就去Nginx中去匹配,在实际改造中这样不现实,还是配置静态资源访问吧
        如果有更好的解决方案请在下方评论!!!
    -->
    location ~ .(gif|jpg|png|js|css)$ {
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://10.3.1.1:18080;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    4、Nginx Http和Https共存

    <!-- 
        目前的Http和Https共存,我暂时是单独配置一个Http Server 和一个Https Server两个端口监听互不干扰,同样如果有更好的方案请在下方评论告诉我!!!
     -->
    server {
            listen 80;
            server_name  10.3.1.2;
    
            location / {
                root html;
                index  index.html index.htm;
            }
    
            location = /bms_core/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host:$server_port;
                proxy_pass http://10.3.1.1:18080;
                #return 302 http://10.3.1.2:18080/bms_core;
            }
            location ~ .(gif|jpg|png|js|css)$ {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://10.3.1.1:18080;
    
            }
    
            location = /bms {
                return 302 http://10.3.1.2:18080/bms_core;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    5、我的nginx.conf配置文件的完整配置

    worker_processes  4;
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /usr/local/nginx/logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
    
        upstream my_server{
            server 10.3.1.2:3128 weight=5 ;
            server 10.3.1.2:80 weight=1;
         }
    
        server {
            listen 80;
            server_name  10.3.1.2;
    
            location / {
                root html;
                index  index.html index.htm;
            }
    
            location = /bms_core/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host:$server_port;
                proxy_pass http://10.3.1.1:18080;
                #return 302 http://10.3.1.2:18080/bms_core;
            }
            location ~ .(gif|jpg|png|js|css)$ {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://10.3.1.1:18080;
    
            }
    
            location = /bms {
                return 302 http://10.3.1.2:18080/bms_core;
            }
        }
    
        server {
            listen       443 ssl;
            server_name  10.3.1.2;
    
            ssl_certificate      /usr/local/nginx/conf/server.crt;
            ssl_certificate_key  /usr/local/openssl/bin/nopass-server.key;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            server_tokens off;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            location = /xxx {
                return 302 http://10.3.1.2:18080/bms_core;
            }
    
            location = /bms_core/ {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://10.3.1.1:18080;
            }
    
            location ~ .(gif|jpg|png|js|css)$ {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://10.3.1.1:18080;
            }
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    四、验证Client到Nginx Https请求是否生效,Https请求中是否建立SSL握手

    1、验证Https模块和Https Server配置是否生效

    (一)通过使用Https请求Nginx

    这里写图片描述

    (二)需要注意的点

    • 使用OpenSSL进行自签证书,所得证书是不能被浏览器所信任的。
    • 如果实际业务并没有浏览器与服务端交互,那么就可以使用OpenSSL进行自签证书,签发证书的目的只是为了单纯的使用Https请求来提高数据安全性,关于Https为什么安全,请参考下面博客。

      Https参考博客

      SSL/TLS握手参考博客

    • 如果需要使用到受浏览器信任的CA证书,可以参考下面博客对CA证书签发机构的介绍,自己选择哪个签来机构来签发自己的受信任CA证书。

      收费证书申请推荐

      免费证书签发机构参考博客

    2、Tcpdump使用

    参考博客

    #查看网卡名命令
    ifconfig
    
    #通过tcpdump抓包
    #eth0为网卡名
    #host 10.2.1.254为发起请求的客户端ip地址
    #-w ./eth1.cap是将抓包信息输出到指定目录下指定文件中
    tcpdump -i eth0 host 10.2.1.254 -w ./eth1.cap
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3、Wireshark使用

    参考博客

    (一)查看抓包信息

    这里写图片描述

    在Wireshark中点击文件,然后打开抓包文件。

    (二)查看TCP传输流

    这里写图片描述

    点击请求,然后右键追踪TCP流。

    4、抓取Http请求

    (一)查看是否发起Http请求

    这里写图片描述

    (二)追踪TCP流,查看数据包在TCP传输过程中是否是明文

    这里写图片描述

    5、抓取Https请求

    (一)查看是否建立握手

    这里写图片描述

    (二)追踪TCP流,查看数据包是否加密

    这里写图片描述

    6、结论

    (一)Http

    ​ 通过Client向服务端发起的Http请求看到,客户端向服务端发起Http请求,并且通过追踪TCP流可以看到数据是明文传输没有被加密。

    (二)Https

    ​ 通过Client向服务端发起的Https请求看到,中间建立了TLS握手,并且通过追踪TCP流可以看到数据是使用对称加密后的数据。

  • 相关阅读:
    浅谈页面的瀑布流布局
    前端常用动画库
    JavaScript七宗罪和一些槽点
    prototype与 _proto__的关系
    Javascript之傻傻理不清的原型链、prototype、__proto__
    C#开发微信门户及应用(26)-公众号微信素材管理
    C#开发微信门户及应用(25)-微信企业号的客户端管理功能
    基于InstallShield2013LimitedEdition的安装包制作
    Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
    Entity Framework 实体框架的形成之旅--Code First的框架设计(5)
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/9562605.html
Copyright © 2020-2023  润新知