Apache + Tomcat: Using mod_proxy instead of AJP
Asked Answered
C

3

21

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 apache:

ProxyPass /path http://localhost:8080/path

But it comes through as localhost, which is useless when we have a bunch of sites on the same server. I could set the host manually in the server config:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           proxyName="pretend.host" proxyPort="80" />

But that again doesn't serve more than one site. And I don't like the idea of using a different internal port for each site, that sounds really ugly.

Is there no way to transfer the port when I proxy it?

(If you ask why I don't just use AJP, the answer is this error. I'm trying everything I can before giving up on Tomcat and Apache entirely)

Commandant answered 5/6, 2009 at 15:5 Comment(0)
S
40

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.

Senhauser answered 7/6, 2009 at 22:5 Comment(2)
How would you cater for static files? I.e. how would you tell Apache to server everything requested from /static how would you set the document root for this?Olenta
@Olenta - ProxyPassMatch should help you : httpd.apache.org/docs/current/mod/mod_proxy.html#proxypassmatchSenhauser
B
5

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.

Beadruby answered 5/6, 2009 at 16:1 Comment(1)
We are of course using virtual hosts in Apache, but I was using localhost in the ProxyPass directive. Using the appropriate domain instead is acceptable, but not quite perfect - it overrides the domain on which the request came in, information which is then used down the line.Commandant
L
0

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>
Longe answered 26/8, 2016 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.