Spring Boot with embedded Tomcat behind Apache proxy

前端 未结 7 1387
暖寄归人
暖寄归人 2020-12-08 09:51

We have a Spring Boot (Spring MVC) app with embedded Tomcat on a dedicated appserver behind an Apache SSL proxy.

The SSL port on the proxy server is 4433, forwarding

7条回答
  •  甜味超标
    2020-12-08 10:42

    I had the same problem the other day. After some debugging of Spring Boot 1.3 I found the following solution.

    1. You have to setup the headers on your Apache proxy:

    
        ServerName www.myapp.org
        ProxyPass / http://127.0.0.1:8080/
        RequestHeader set X-Forwarded-Proto https
        RequestHeader set X-Forwarded-Port 443
        ProxyPreserveHost On
        ... (SSL directives omitted for readability)
    
    

    2. You have to tell your Spring Boot app to use these headers. So put the following line in your application.properties (or any other place where Spring Boots understands properties):

    server.use-forward-headers=true
    

    If you do these two things correctly, every redirect your application sends will not go to http://127.0.0.1:8080/[path] but automatically to https://www.myapp.com/[path]

    Update 1. The documentation about this topic is here. You should read it at least to be aware of the property server.tomcat.internal-proxies which defines the range of IP-addresses for proxy servers that can be trusted.

提交回复
热议问题