关于nginx+tomcat https的部署以前网上一直有2种说法:node
1.nginx和tomcat都要部署ssl证书nginx
2.nginx部署ssl证书,tomcat增长ssl支持apache
在实际的部署过程当中nginx的配置和ssl证书部署很顺利,可是tomcat用网上流传的说法配置以后,发现非静态内容没法正常使用。(项目用了dubbox,dubbox基于resteasy发布rest协议)浏览器
通过一系列折腾,最终总结出如下经验,但愿能帮到其余人。tomcat
一. 申请SSL证书服务器
SSL证书其实能够本身生成,可是由于本身生成的证书不是浏览器厂商承认的受信机构颁发,因此浏览器会打红叉!为了用户体验,因此session
咱们要到SSL证书厂商那里申请SSL证书。我的推荐 globalsign(淘宝用的) wosign.com(价格优惠) startssl(有免费版)。若是使用单域名DV证书,签发只要15分钟-2小时。厂商一般会把签发好的证书以邮件形式发送过来。dom
1. 本身生成 证书签名请求(CSR)和私钥(KEY)。能够使用厂商提供的工具https://www.trustasia.com/tools/csr-generator/ 工具
也能够使用openssl生成 openssl req -new -nodes -newkey rsa:2048 -keyout domain.key -out domain.csr spa
2.把 证书签名请求(CSR)和私钥(KEY)提供给厂商。
3.收到厂商签发好的证书。(私钥仍是本身的)
二.部署证书到nginx并配置
nginx的证书部署相对简单。
1.肯定nginx是否安装了ssl模块,若是没有须要从新mark install。(建议 pcre-8.38 zlib-1.2.8 openssl-1.0.2g 版本过高可能致使编译失败)
2.把厂商提供的 x.crt 内容追加到 x.cer, 而后把 x.key 和 x.cer 放到Nginx服务器
3.nginx.conf 增长https支持。主要是配置 80 和 443 端口的监听
server { listen 80; server_name x; rewrite ^(.*)$ https://$host$1 permanent; } server { listen 443; server_name x; ssl on; ssl_certificate /etc/nginx/server.cer; ssl_certificate_key /etc/nginx/server.key; ssl_session_timeout 5m; ssl_protocols TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { client_max_body_size 16m; client_body_buffer_size 128k; proxy_pass http://online/; proxy_set_header Host $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 https; proxy_next_upstream off; proxy_connect_timeout 30; proxy_read_timeout 300; proxy_send_timeout 300; } }
我这里作了http强跳到https,不须要这个功能,能够去掉 rewrite ^(.*)$ https:
三.tomcat增长对https协议的支持
1.Connector节点加入 redirectPort="443" proxyPort="443"
2.加入新的Value节点 <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="x-forwarded-for" remoteIpProxiesHeader="x-forwarded-by" protocolHeader="x-forwarded-proto"/>