https

nginx系列-06-nginx配置https并转发请求至后端t

2016-11-13  本文已影响424人  hylexus

[TOC]

前言

上一篇,我们介绍了nginx启用https,但是使用的证书是私有CA颁发的。
这种私有证书出来个人练习或者在内部使用外,还真不知道有什么其他用途。

现在,我们来体验体验真正的商用https证书。

1 环境准备

2 申请https证书

此处使用的是https://startssl.com/提供的免费https证书。

这部分有疑问的话,可以参考本人另一篇文章:http://blog.csdn.net/hylexus/article/details/53150333

# 本人申请证书后下载得到了一个hyl.xxx.tech.zip的压缩包
# 其中hyl.xxx.tech应该会用你自己的域名代替

# 该文件内容如下:

[root@hylexus https]# tree
.
├── ApacheServer.zip # apache/httpd
├── IISServer.zip # MS-IIS
├── NginxServer.zip # Nginx
└── OtherServer.zip # 其他服务器

这里我们使用NginxServer.zip中的文件进行后续操作.

# 解压后得到文件:1_hyl.xxx.tech_bundle.crt

# 为方便后续操作,将该文件重命名为nginx.crt
# 并将其移动至nginx配置文件目录下新建的ssl目录下


mkdir /etc/nginx/ssl
# 证书文件nginx.crt
cp 1_hyl.xxx.tech_bundle.crt /etc/nginx/ssl/nginx.crt

# 应用程序秘钥nginx.key
# 名字随意,这个文件是你自己生成CSR的时候用的秘钥文件
cp nginx.key /etc/nginx/ssl/nginx.key

3 nginx启用https

此时的ssl目录:

[root@hylexus ssl]# pwd
/etc/nginx/ssl
[root@hylexus ssl]# tree
.
├── nginx.crt # 申请的https证书
└── nginx.key # 应用程序私钥

基于域名的虚拟主机配置

server{
    # 同时支持http和https
    listen  80;
    listen  443 ssl;
    server_name hyl.xxx.tech;
    access_log  /var/log/nginx/hyl.xxx.tech.access.log;

    keepalive_timeout   70;

    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
    ssl_certificate     /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 10m;

    location / {
        #root   /usr/share/nginx/html;
        index  dashboard index;
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host       $http_host;
        proxy_set_header X-Real-IP $remote_addr;  
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        proxy_set_header X-Forwarded-Proto $scheme;
    }

}

4 tomcat-server.xml配置

Connector等的配置按自己的喜好来
此处应该注意的地方是在你的虚拟主机下加一个 Valve

# 注意几个请求头和nginx虚拟主机的配置中应该是对应的
# X-Forwarded-For、X-Forwarded-Proto等
<Valve className="org.apache.catalina.valves.RemoteIpValve" 
    remoteIpHeader="X-Forwarded-For" 
    protocolHeader="X-Forwarded-Proto" 
    protocolHeaderHttpsValue="https"/>

5 效果预览

没有警告信息的https
上一篇 下一篇

猜你喜欢

热点阅读