Apache Proxy – How to Pass a Request from One Apache Server to Another

apache-2.2mod-proxymod-rewritereverse-proxy

I have two Apache Servers with mod_proxy enabled. I want to know how to "pass the request" from "Apache Server A" to "Apache Server B" using the same port (80).

In "server A" with internal ip: 192.168.0.5 I have configured DNS and I am also using it as my mail server.

In "server B" with internal ip: 192.168.0.10 I have my own cloud server.

Today when I access wwww.mydomain.com or www.mydomain.com/webmail everything works because the content is all in "server A", what I need is when somebody tries to access owncloud.mydomain.com they can access my server B without redirecting them to another port like owncloud.mydomain.com:81, I just want to use port 80.

Actually I get a redirect loop because when I try to redirect I guess "SERVER A" is getting the same request by himself. I have set my virtualhost working in the same "SERVER A" what I want is to "redirect" to another server using the same port and URL (owncloud.mydomain.com).

I don't want to redirect using another port.

Best Answer

You're going to want to do something like this on Server A:

NameVirtualHost *
<VirtualHost *>
    ServerName owncloud.mydomain.com

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://192.168.0.10:80/
    ProxyPassReverse / http://192.168.0.10:80/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

Depending on your application you may need to make use of one or all of the following:

  • X-Forwarded-For - The IP address of the client.
  • X-Forwarded-Host - The original host requested by the client in the Host HTTP request header.
  • X-Forwarded-Server - The hostname of the proxy server.

Take a look at the mod_proxy documentation for more tips and tricks.

References