Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache + Tomcat: Using mod_proxy instead of AJP

Tags:

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)

like image 366
Marcus Downing Avatar asked Jun 05 '09 15:06

Marcus Downing


People also ask

Is AJP better than HTTP?

HTTP connectors can also be used as part of a load balancing scheme, in conjunction with an HTTP load balancer that supports session stickiness, such as mod_proxy. However, as AJP tends to handle proxying better than HTTP, this usage is not common.

What is difference between mod_jk and mod_proxy?

Which connector: mod_jk or mod_proxy? mod_jk is mature, stable and extremely flexible. It is under active development by members of the Tomcat community. mod_proxy_ajp is distributed with Apache httpd 2.2 and later.

What is AJP protocol in Tomcat?

Apache JServ Protocol (AJP) is used for communication between Tomcat and Apache web server. This protocol is binary and is enabled by default. Anytime the web server is started, AJP protocol is started on port 8009. It is primarily used as a reverse proxy to communicate with application servers.

What is mod_proxy used for?

mod_proxy and related modules implement a proxy/gateway for Apache HTTP Server, supporting a number of popular protocols as well as several different load balancing algorithms. Third-party modules can add support for additional protocols and load balancing algorithms.


2 Answers

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.

like image 162
Robert Munteanu Avatar answered Oct 08 '22 22:10

Robert Munteanu


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.

like image 28
gareth_bowles Avatar answered Oct 08 '22 22:10

gareth_bowles