Apache mod_proxy

By eric

I came up against the interesting problem of putting multiple stand alone apache tomcat instances with different virtual host names on the same machine that all needed to be accessible via port 80 (on the same IP). There is always mod_jk, but that seems like a bit too much to fix a simple problem. Being a strong believer in the right tool for the right job, I came across mod_proxy. This way I get to take advantage of apache connection handling without having to put a whole proxy server in front of it. Because there is dispatching by virtual host to do, putting apache in front just seemed to be the best idea.

Since there aren’t too many clear HOWTOs on this, it took a bit of fudging. Here is what you need to know.

Let’s create the host http://port8080.lubow.org/ to go to http://8080.lubow.org:8080/.

The first thing is a fairly common default configuration of NameVirtualHost option. This is so you can have multiple virtual hosts per IP. Unless you are crazy (or have a really good reason), you do not want to create an open proxy. So you need to globally configure the ProxyRequests variable to be off. Do the base setup for a VirtualHost of ServerName and ServerAdmin.

Setup the proxy authorizations (similar to the apache allow/denys). In order for the right HTTP headers to make it to the proxy’d virtual host, the headers will need to be rewritten. This needs to happen both going to the host and coming back from the host going to the client. This is why there is the ProxyPass and ProxyPassReverse. The first argument is the URL that on the virtual host that should match the URL (second argument) on the proxy’d virtual host. The ProxyPreserveHost option is generally not needed (but it is for the specific application I am running. Click the link above to read the description to determine whether it is right for you.

Putting it all together, you will get a file that looks like below. Make sure you replace your IPs and hostnames with what’s appropriate for your environment.

ProxyRequests Off

     ServerAdmin webmaster@lubow.org
         ServerName port8080.lubow.org
         <proxy *>
                  Order deny,allow
                  Allow from all
         ProxyPreserveHost   On
         ProxyPass   / http://8080.lubow.org:8080/
         ProxyPassReverse    / http://8080.lubow.org:8080/
  • crirus


    I've got the strangest thing ever.. when I set ProxyRequests Off my mysql is going at 70% of CPU.
    I am forced to keep the apache proxy on.
    Any hints?

    • While MySQL is going at 70%, what is happening? If you go into MySQL and type either “SHOW PROCESSLIST” or “SHOW FULL PROCESSLIST”, what does it say is happening? Are there a lot of open connections made and kept open by your application if you turn proxy off?

      • crirus

        I can't tell for sure, the load increase almost instantly, I can barely ssh in to kill the web server. As soon as I do that the load goes down.

        Very strange stuff, I tested same idea on a different box and everything is just fine there, lighttpd receive the proxy requests just fine.

        No idea what happened to this first box.

        • If you can, log in via SSH to MySQL and then when you restart apache do a
          “SHOW FULL PROCESSLIST” and wait for the outcome. Also leave a terminal
          open with the command to kill apache already typed out so all you have to do
          it hit enter after you get the response from “SHOW FULL PROCESSLIST”.
          Although your issue may only be relegated to the one machine, it would still
          be handy/interesting to know what caused in case you run into it again in
          the future.

  • crirus

    missed the reply button below

Follow My Travels

Buy My Book


  • 2017
  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006