Skip to main content

Apache VirtualHost Proxy Configuration - Helpful for Tomcat, Node.js and similar frameworks

I recently came across this question on ServerFault:
" I've subsonic application running of [sic] tomcat. Everything else works on apache. I don't want to write port number everytime [sic] so I'd like to set-up [sic] a simple directory where subsonic will be accessible. So, I'm trying to make virtualhost file [sic] inside apache dir. [...] I tried many variations, but cannot make anything work. "
The poor chap than [sic - ha!] provided an example of his latest go at the problem, an excerpt from his httpd.conf file:

<VirtualHost *:80>
     DocumentRoot /var/www/streamer
     ProxyPass               /       http://mini.local:4040/
     ProxyPassReverse        /       http://mini.local:4040/
Not sure a bad go of it, all thing considered. Still, it wasn't providing him with the sort of results he was looking for. Naturally I had encountered similar issues not long ago myself, with the implementation of a Ghost blogging software platform, which runs on node.js. Its been my first serious effort with node, other than some occasionally one-off one-page type of scripts for existing web sites.

So, I felt like I might be able to help the gentleman. Now, let's bear in mind his question: "I don't want to write port number everytime". He does not say, "I never want to write port number", just that it should be possible to render the page with a URL that does not append the port number. Ensuring that the port number is never written would require the solution below in addition to another step - there are a few ways to resolve it, but mod_rewrite would be the most obvious / most well known. Some of the other solutions might depend upon what version of Apache is being used, like an <if> directive for example.

In any event, here is what I provided to the questioner, which is quite similar to what I have implemented previously and serves my purposes quite well (notice I am using his somewhat strange hostname nomenclature):

<VirtualHost *:80>
    ServerName mini.local
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
            AddDefaultCharset Off
            Order deny,allow
            Allow from all
    ProxyPass / http://mini.local:4040/
    ProxyPassReverse / http://mini.local:4040/