Tomcat behind Apache using ajp for Spring Boot application

后端 未结 3 1532
感情败类
感情败类 2020-12-29 11:04

I\'ve been trying to configure Apache web server with a Spring Boot app that uses embedded Tomcat. Before Spring Boot I used to create an ajp.conf file like:



        
相关标签:
3条回答
  • 2020-12-29 11:31

    Had a similar problem but with HTTP-Proxy. After some debugging of Spring Boot 1.3 I found the following solution. It should be similar for the AJP Proxy.

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

    <VirtualHost *:443>
        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)
    </VirtualHost>
    

    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.

    0 讨论(0)
  • 2020-12-29 11:39

    Configurable throught properties or yml file.

    @Configuration
    @ConfigurationProperties(prefix = "tomcat")
    public class TomcatConfiguration {
       private int ajpPort = 8009;
    
       private boolean ajpAllowTrace = false;
       private boolean ajpSecure = false;
       private String ajpScheme = "http";
       private boolean ajpEnabled;
    
    
      @Bean
      public EmbeddedServletContainerFactory servletContainer() {
    
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        if (isAjpEnabled()) {
            Connector ajpConnector = new Connector("AJP/1.3");
            ajpConnector.setProtocol("AJP/1.3");
            ajpConnector.setPort(getAjpPort());
            ajpConnector.setSecure(isAjpSecure());
            ajpConnector.setAllowTrace(isAjpAllowTrace());
            ajpConnector.setScheme(getAjpScheme());
            tomcat.addAdditionalTomcatConnectors(ajpConnector);
        }
    
        return tomcat;
        }
    // ... Get/Set
    }
    

    application.yml

    tomcat:
      ajpEnabled: true
      ajpPort: 9009
      ...
    
    0 讨论(0)
  • 2020-12-29 11:40

    Deduced from the comments above:

    @Configuration
    public class TomcatAjpConfig {
    
    @Bean
    @SuppressWarnings("static-method")
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addAdditionalTomcatConnectors(createConnector());
        tomcat.addContextValves(createRemoteIpValves());
        return tomcat;
    }
    
    private static RemoteIpValve createRemoteIpValves() {
        RemoteIpValve remoteIpValve = new RemoteIpValve();
        remoteIpValve.setRemoteIpHeader("x-forwarded-for");
        remoteIpValve.setProtocolHeader("x-forwarded-proto");
        return remoteIpValve;
    }
    
    private static Connector createConnector() {
        Connector connector = new Connector("AJP/1.3");
        connector.setPort(8009);
        return connector;
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题