Apache forward proxy – how to override a destination address

apache-2.2mod-proxyPROXYrewrite

I'm running apache as a forward proxy to do header injection, URL manipulation, and request logging for some Android apps and I'd need to override the destination for some requests.

For example, www.example.com would have an IP address of 1.2.3.4 but I need to divert the traffic to 4.3.2.1 without changing the application or affecting any other apps running on the apache server.

I'd also need the flexibility to override the address under different conditions, which means that manipulating /etc/hosts is not an option.

The clients are apps running on Android emulators which are configured to use apache proxy instances running on the host machine.

So far I have a setup like this:

## conf.d/proxy_a.conf
## proxy_b.conf, proxy_c.conf listen on different ports with different mappings / headers

Listen localhost:16002

<VirtualHost localhost:16002>
        ProxyRequests On
        ProxyPreserveHost On
        RewriteEngine On

        <Proxy "http://www.example.com/*">
                Order deny,allow
                Deny from all
                Allow from localhost

                RequestHeader set host "www.example.com"
                RewriteRule ^/?(.*) http://4.3.2.1/$1 [NE]
        </Proxy>

        <Proxy *>
                Order deny,allow
                Deny from all
                Allow from localhost
        </Proxy>

        ## also tried, doesn't work
        #RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
        #RewriteRule ^/?(.*) http://4.3.2.1/$1 [NE]

        RequestHeader set x-custom-header "abcdefgh"

        RequestHeader merge user-agent "proxy16002"

</VirtualHost>

I'm aware that the functionality I'm looking for is easily achievable with a reverse proxy, but I'm looking for a solution using a straight proxy (no need to modify /etc/hosts or applications).

What's missing? Is this even possible with apache (2.2)?

Best Answer

This may not be what you're wanting, but you could set up another Apache proxy such as in a virtual machine and modify the /etc/hosts file there. Then traffic would travel through two proxies - the one you have now followed by the one you set up on the virtual machine. Not ideal I know, but a possible solution if nobody comes up with something better.

Alternately you could modify Apache source and recompile it, but that's not ideal either.