背景介绍:

         HTTPS,全称:Hyper Text Transfer Protocol over Secure Socket Layer,是通过SSL安全机制传输,保证网站数据不被窃听、冒充、篡改等安全风险,保证网站传输安全可靠,下面是通过nginx配置ssl模块记录。

 

环境信息:

 nginx ssl--tomcat1

                      tomcat2

                      tomcat3

 

操作系统: centos  6.8

nginx 1.12.0

tomcat tomcat8

 

        之前的流量图:普通用户,从公网访问公司网站,会经过防火墙进入nginx agent反向代理,反向代理根据用户的域名,分配到对应的web服务器。

 

 

        SSL流量:使用证书访问网站,当用户的流量到达nginx agent反向代理后,nginx开启ssl加密,然后转向到对应的web服务器,web服务器程序认证完毕后返回给用户对应的http连接。

        流量配置图如下:

 

 wKiom1mi0KHRE0YVAABXt1tH1OI326.png-wh_50

 


 

 

开启SSL证书准备

 

1.需要准备nginx已经开启了ssl模块

 

             检查命令如下,发现有此模块,如果没有,需要重新编译 --with-ssl模块安装

 

[root@kmsdT-MQ ~]#  nginx -V | grep ssl

nginx version:  nginx/1.12.0

built by gcc 4.8.5  20150623 (Red Hat 4.8.5-4) (GCC)

built with OpenSSL  1.0.1e-fips 11 Feb 2013

TLS SNI support  enabled

configure  arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf  --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  --lock-path=/var/run/nginx.lock  --http-client-body-temp-path=/var/cache/nginx/client_temp  --http-proxy-temp-path=/var/cache/nginx/proxy_temp  --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp  --user=nginx --group=nginx --with-compat --with-file-aio --with-threads  --with-http_addition_module --with-http_auth_request_module  --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module  --with-http_mp4_module --with-http_random_index_module  --with-http_realip_module --with-http_secure_link_module  --with-http_slice_module --with-http_ssl_module  --with-http_stub_status_module --with-http_sub_module --with-http_v2_module  --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module  --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2  -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong  --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'  --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

[root@kmsdT-MQ ~]#

 

 

2.准备好证书文件

          通过key证书文件,向第三方机构申请认证,申请证书,证书文件命名如下:

www.vbeard.cc.cer    #公钥

www.vbeard.cc.key   #私钥

 

 

 

nginx配置

 

2.1 使用了nginx 1.12版本,配置文件按照模块分开配置了

 

       编辑vi /etc/nginx/nginx.conf , http里面增加下面配置

 upstream yc{

   server 192.168.120.101:87  max_fails=3 fail_timeout=30s;

    server 192.168.120.108:87 max_fails=3 fail_timeout=30s;

  server 192.168.120.105:87  max_fails=3 fail_timeout=30s;

}

 

 

 

2.2  编辑80默认虚拟主机配置文件,增加易创网和服务中心域名对应的虚拟主机

 

[root@kmsdT-MQ ~]#  cat /etc/nginx/conf.d/default.conf

server {

    listen       80;

    server_name  www.vbeard.cc;

 

    #charset koi8-r;

      #access_log  /var/log/nginx/log/host.access.log  main;

 

    rewrite  ^/Home/Login(.*) https://www.vbeard.cc/$1 permanent;     #nginx根据头判断是否走https

 

    location / {

           proxy_pass http://yc;     #反向代理到后端易创网,易创网可能有多台,起到负载均衡的作用

            proxy_set_header Host $host;

            proxy_set_header X-Forwarded-For  $remote_addr;   

 

 

    }

 

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root    /usr/share/nginx/html;

    }

}

 

2.3 编辑ssl.conf配置文件,443虚拟主机配置

        如下,通过反向代理到后端IIS web对应的应用服务器,对应的应用服务器处理后,如果登录成功,则返回http给最终用户。

[root@kmsdT-MQ ~]#  cat /etc/nginx/conf.d/ssl.conf

server {

 

        listen       443;

        server_name   www.vbeard.cc ;

        ssl                  on;           #开启ssl证书加密

        ssl_certificate      www.vbeard.cc.cer;

         ssl_certificate_key  www.vbeard.cc.key;

            ssl_session_cache    shared:SSL:10m;

        ssl_session_timeout  5m;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_ciphers  HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers   on;

 

 

    location / {

          proxy_pass http://yc;     #当接收到对应请求后,转向到后端服务器处理。

            proxy_set_header Host $host;

            proxy_set_header X-Forwarded-For  $remote_addr;   

 

    }

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root    /usr/share/nginx/html;

    }

}

}

 

 

 

2.4 nginx重新加载

        检查nginx配置没有问题,就执行下面方式重新加载配置文件。

 

[root@kmsdT-MQ ~]# nginx -t         #检查nginx配置语法

nginx: the  configuration file /etc/nginx/nginx.conf syntax is ok

nginx:  configuration file /etc/nginx/nginx.conf test is successful

[root@kmsdT-MQ ~]# nginx -s reload      #重新加载nginx配置

[root@kmsdT-MQ ~]#

 

 

测试

        需要更改本地host后,测试登录是否加密了。

对应修改本地hosts方法:C:\Windows\System32\Drivers\etc\hosts

 

192.168.121.106 www.vbeard.cc

 

访问: https://www.vbeard.cc 检查是否可以正常打开。