Nginx + Tomcat + HTTPS 配置不需要在 Tomcat 上启用 SSL 支持

来源:互联网 发布:淘宝卖家账号怎么注销 编辑:程序博客网 时间:2024/06/05 08:34

下面是详细的配置(Nginx 端口 80/443,Tomcat 的端口 8080):
Nginx 这一侧的配置没什么特别的:
upstream tomcat {    server 127.0.0.1:8080 fail_timeout=0;}# HTTPS serverserver {    listen       443 ssl;    server_name  localhost;    ssl_certificate      /Users/winterlau/Desktop/SSL/oschina.bundle.crt;    ssl_certificate_key  /Users/winterlau/Desktop/SSL/oschina.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">    <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">            <Valve className="org.apache.catalina.valves.RemoteIpValve"                  remoteIpHeader="x-forwarded-for"                  remoteIpProxiesHeader="x-forwarded-by"                  protocolHeader="x-forwarded-proto"            />            <Context path="" docBase="/oschina/webapp" reloadable="false"/>      </Host>    </Engine>  </Service></Server>


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