Nginx+Tomcat配置https

Nginx + Tomcat 配置 HTTPS

1、总述

浏览器和 Nginx 之间走的 HTTPS 通讯,而 Nginx 到 Tomcat 通过 proxy_pass 走的是普通 HTTP 连接。

2、Nginx配置(nginx.conf),部分

http {
    #HTTPS server
    server {
        listen       443 ssl;
        server_name  goldlone.cn;
        
        #证书地址
        ssl_certificate      ./1_goldlone.cn_bundle.crt;
        ssl_certificate_key  ./2_goldlone.cn.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://127.0.0.1:8080;
        }
    }
}

3、Tomcat配置(server.xml),部分

<Service name="Catalina">

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443"
               proxyPort="443" />

    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               remoteIpHeader="x-forwarded-for"
               remoteIpProxiesHeader="x-forwarded-by"
               protocolHeader="x-forwarded-proto"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>

  </Service>

4、结束

修改配置文件时,注意红色部分

证书是腾讯云申请的免费证书:https://console.qcloud.com/ssl

 

posted @ 2017-07-31 16:37  goldlone  阅读(933)  评论(0编辑  收藏  举报