Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HTTP Proxy Based on Hostname

Previously, I had Apache HTTP set up correctly to forward incoming HTTP requests on port 80 to my Tomcat instance running on port 8080. However, now I am in such a situation where I need to be able to proxy incoming requests on port 80 to either Tomcat @ 8080 or a different process @ 9000, depending on hostname. Below is a snippet of my attempt at setting up my Apache configuration to handle such a case:

<VirtualHost *:80>
  ServerName hostname1
  ProxyPreserveHost On
  ProxyPass / http://hostname1:8080/
  ProxyPassReverse / http://hostname1:8080/
</VirtualHost>
<VirtualHost *:80>
  ServerName hostname2
  ProxyPreserveHost On
  ProxyPass / http://hostname2:9000/
  ProxyPassReverse / http://hostname2:9000/
</VirtualHost>

Now when requesting from either hostname1 or hostname2, I get an instant 500 apparently due to mod_proxy not finding any matching rules to proxy the request:

[Fri Feb 08 06:41:01 2013] [warn] proxy: No protocol handler was valid for the URL /. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.

Note that Tomcat is not receiving the incoming requests, and thus no log output

For sanity's sake, I checked that I indeed can access these two resources individually at their respective ports, i.e. I can access hostname1 using hostname1:8080 and hostname2 using hostname2:9000.

Are there things missing here to help me configure this correctly?

Additionally, are there any better ways of managing this in perhaps a more sane manner?

Thank you for any help!

like image 625
jerluc Avatar asked Feb 08 '13 14:02

jerluc


2 Answers

The answer seemed to be a faulty configuration regarding a separate module, namely mod_proxy_http. I came to the following serverfault answer helped me, as tipped off by the "proxy: No protocol handler was valid for the URL" message in the Apache HTTP error logs: https://serverfault.com/questions/242650/setting-up-a-basic-mod-proxy-virtual-host

like image 57
jerluc Avatar answered Sep 27 '22 23:09

jerluc


If you want to configure name-based virtual hosts, add the NameVirtualHost directive.

Add ServerName and DocumentRoot to each VirtualHost.

Example:

NameVirtualHost *:80

<VirtualHost *:80>
 ServerName hostname1
 DocumentRoot /www/hostname1
 ProxyPreserveHost On
 ProxyPass / http://hostname1:8080/
 ProxyPassReverse / http://hostname1:8080/
</VirtualHost>

<VirtualHost *:80>
  ServerName hostname2
  DocumentRoot /www/hostname2
  ProxyPreserveHost On
  ProxyPass / http://hostname2:9000/
  ProxyPassReverse / http://hostname2:9000/
</VirtualHost>
like image 40
user2055857 Avatar answered Sep 28 '22 00:09

user2055857