前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx+SSL+Tomcat配置SSL证书(Nginx+SSL做前端,后端使用tomcat)

Nginx+SSL+Tomcat配置SSL证书(Nginx+SSL做前端,后端使用tomcat)

作者头像
BUG弄潮儿
发布2022-06-30 14:53:52
1.7K0
发布2022-06-30 14:53:52
举报
文章被收录于专栏:JAVA乐园

下面解决方案是浏览器和 Nginx 之间使用 HTTPS 通讯,而 Nginx 到 Tomcat 通过 proxy_pass 使用标准 HTTP 连接。

详细配置文件如下(Nginx 端口 80/443,Tomcat 端口 8080):

Nginx 前端SSL配置文件:

upstream tomcat {

server 127.0.0.1:8080 fail_timeout=0;

}

# HTTPS server

server {

listen 443 ssl;

server_name localhost;

ssl_certificate /Users/SSL/chinassl.crt;

ssl_certificate_key /Users/SSL/chinassl.key;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_set_header X-Forwarded-Proto https;

proxy_redirect off;

proxy_connect_timeout 240;

proxy_send_timeout 240;

proxy_read_timeout 240;

# note, there is not SSL here! plain HTTP is used

proxy_pass http://tomcat;

}

}

其中最为关键的就是 ssl_certificate 和 ssl_certificate_key 这两项配置,其他的按正常配置。不过多了一个 proxy_set_header X-Forwarded-Proto https; 配置。

后端 Tomcat,配置文件如下 server.xml:

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">

<Service name="Catalina"&gt;

<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="443"

proxyPort="443">

<Engine name="Catalina" defaultHost="localhost>

<Host name="localhost" appBase="webapps"

unpackWARs="true" autoDeploy="true"&gt;

<Valve className="org.apache.catalina.valves.RemoteIpValve"

remoteIpHeader="x-forwarded-for"

remoteIpProxiesHeader="x-forwarded-by"

protocolHeader="x-forwarded-proto" />

<Context path="" docBase="/chinassl/webapp" reloadable="false">

</Host>

</Engine>

</Service>

</Server>

上述的配置中没有什么特别的,但是特别特别注意的是必须有 proxyPort="443",这是整篇文章的关键,当然 redirectPort 也必须是 443。同时 节点的配置也非常重要,否则你在 Tomcat 中的应用在读取 getScheme() 方法以及在 web.xml 中配置的一些安全策略会不起作用。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-01-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 BUG弄潮儿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
SSL 证书
腾讯云 SSL 证书(SSL Certificates)为您提供 SSL 证书的申请、管理、部署等服务,为您提供一站式 HTTPS 解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档