Apache + Tomcat: Using mod_proxy instead of AJP

前端 未结 3 1770
情书的邮戳
情书的邮戳 2020-12-02 07:34

Is there any way I connect Apache to Tomcat using an HTTP proxy such that Tomcat gets the correct incoming host name rather than localhost? I\'m using this directive in apac

相关标签:
3条回答
  • 2020-12-02 08:09

    I think your best bet if you want multiple sites on the same server is to use virtual hosts in your Apache configuration. Here's an example:

    <VirtualHost *:80>
    ServerName server.domain.com
    
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    
    ProxyPass / http://server.domain.com:8080/
    ProxyPassReverse / http://server.domain.com:8080/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
    

    As long as you have server.domain.com registered in your external DNS, the incoming host name will be displayed in client URLs. I'm running a single server hosting 6 separate sites, including 3 that are back by Tomcat, using this method.

    0 讨论(0)
  • 2020-12-02 08:14

    You can still use AJP, and you should since it's faster than HTTP. Just make sure to enable it in http.conf:

    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    

    In that case, this configuration works for me:

    <VirtualHost *:80>
      ServerName public.server.name
    
      ProxyRequests Off
      ProxyPreserveHost On
    
      <Proxy *>
        Order deny,allow
        Allow from all
      </Proxy>
    
      ProxyPass / ajp://localhost:8080/
    # ProxyPassReverse might not be needed,
    # it's only for redirecting from inside.
    #  ProxyPassReverse / ajp://localhost:8080/
    </VirtualHost>
    
    0 讨论(0)
  • 2020-12-02 08:33

    The settings you are looking for are:

    <VirtualHost *:80>
      ServerName public.server.name
    
      ProxyRequests Off
      ProxyPreserveHost On
    
      <Proxy *>
        Order deny,allow
        Allow from all
      </Proxy>
    
      ProxyPass / http://localhost:8080/
      ProxyPassReverse / http://localhost:8080/
    </VirtualHost>
    

    Note that we're using localhost as the proxy target. We can do this since we enable ProxyPreserveHost. The documentation states that

    It is mostly useful in special configurations like proxied mass name-based virtual hosting, where the original Host header needs to be evaluated by the backend server.

    which sounds exactly like what you are doing.

    0 讨论(0)
提交回复
热议问题