Apache as a Reverse Proxy for Virtual Hosts

We all know how to do VirtualHosts on Apache, but what if you want to run a web application inside a container, or on another box, or in a virtual machine?

You need to turn Apache in to a reverse proxy. This is also called a gateway. It’s operation is pretty straightforward, and described in the Apache2 docs:

ProxyPass "/foo" "http://example.com"

That’s pretty simple. When the user hits /foo, they get the pages from example.com.

You can use the ProxyPass directive in a VirtualHost section.

On Debian systems, you set up virtual hosts with separate config files. My current one looks like this:

    ServerName slaptech.net
    ProxyPass "/" "http://example.com/"
    ProxyPassReverse "/" "http://example.com/"

Again, it’s pretty straightforward. I have a domain, slaptech.net, that resolves to the above IP address.

When the Host header matches slaptech.net, it matches this VirtualHost scope. The server then maps the root of the URL, / , to requests at example.com.

See the Apache mod_proxy docs for more details.