tomcat nginx配置总结

系统:ubuntu16.04
nginx,tomcat8.5

方式一:nginx与tomcat均配置https

tomcat转发
conf/server.xml

<!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" proxyPort="8443" />

<!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
         This connector uses the APR/native implementation which always uses
         OpenSSL for TLS.
         Either JSSE or OpenSSL style configuration may be used. OpenSSL style
         configuration is used below.
-->
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol" maxThreads="150" SSLEnabled="true" URIEncoding="UTF-8" >
    <UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
    <SSLHostConfig>
        <Certificate certificateKeyFile="your_privkey.pem"
                     certificateFile="your_cert.pem"
                     certificateChainFile="your_fullchain.pem"
                     type="RSA" />
    </SSLHostConfig>
</Connector>


<!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" enableLookups="false" redirectPort="8443" />

conf/web.xml

<security-constraint>
    <web-resource-collection >
        <web-resource-name >SSL</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>

    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

假设不使用nginx,那么这里,可以将所有的8443改为443。https默认的端口是443。使用上述这种Connector,收到的http://localhost:8080/将会转发到https://localhost:8443/
备注:使用wget请求,将可以看得到请求的变化。

然后对应的nginx,conf/nginx.conf

upstream tomcat_pool
{
    #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
    server 192.168.0.100:8443 weight=4 max_fails=2 fail_timeout=30s;
    server 192.168.0.101:8443 weight=4 max_fails=2 fail_timeout=30s;
}

http{
    省略若干
    server{
        listen 443;
        省略若干
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect   off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://tomcat_pool;    #转向tomcat处理
            # 把 https 的协议告知 Tomcat,否则 Tomcat 可能认为是 http 的请求
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }

    server {
        listen 80;
        server_name 192.168.0.99;
        rewrite ^ https://$server_name$request_uri? permanent;
    }
}

nginx中监听了443端口,所以tomcat就不要使用https默认的443端口。如果不需要nginx转发,那么在tomcat中就可以将8443改成443。另外,在upstream tomcat_pool中,8443也可以写成8080,因为tomcat会自动将8080端口的请求,转发到8443。

方式二:nginx配置https,tomcat配置http

这种方式,不推荐。好不容易配置了https,还有证书啥的,结果因为是nginx的https转发到了只支持http的tomcat,导致浏览器地址栏上不显示“安全”字样。这种的配置,tomcat无需其他额外的配置,只原来的就可以了。另外,对于nginx的配置,有一点,不一样

proxy_pass http://tomcat_pool;    #转向tomcat处理

这里只能用http,不能用https了。

备注:
我有尝试过将nginx监听443,也将tomcat中的8443全部改为443,这时候,nginx与tomcat只能够成功启动一个,另外一个启动失败的原因是:端口占用。

从其他网页得知:
redirectPort,是当tomcat收到了https当请求,那么会自动重定向到此redirectPort对应值的接口。

猜你喜欢

转载自blog.csdn.net/yangyangrenren/article/details/80564808
今日推荐