6

I would like to forward a request in Apache, based on a request header, to a different forward proxy. I think the best would be to use mod_rewrite, but it can use only a reverse proxy defined in the same apache configuration.

I also checked the ProxyRemote property of mod_proxy but it can't be used based on conditions, only based on request url's.

I need something like:

If X-CUSTOM-HEADER is value-1 -> forward request to forward proxy p1 If X-CUSTOM-HEADER is value-2 -> forward request to forward proxy p2

etc.

Din anyone managed to make something like this?

Thanks, Alin

Alin
  • 1,176
  • 1
  • 12
  • 15
  • i am facing a similar problem. i also need a forward request and not a reverse request, because the servers themselves dont hold the resources, but should fetch them from the internet. have you come up with a solution yet? – The Surrican Sep 04 '11 at 12:34
  • this requirement was removed after all, so I didn't had a chance to test the solution in production. – Alin Oct 25 '11 at 20:39

3 Answers3

2

I found a solution, its not really elegant. It involves some adaptation on the second server as well.

It derives from a project where I had a similar problem, but needed the servers to be "fully" (selected by a custom script that uses database resources).

This should at least work (I run my URL through a rewrite map to modify it, I adapted it to use headers using RewriteCond).

# example for server number "5" in your remote proxy network
RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
RewriteRule http://([a-z0-9\.]+)/(.*) http://$1.5.server.yourdomain.com$1 [P] 
ProxyRemoteMatch .*\.5\.server\.yourdomain\.com.* http://5.server.yourdomain.com:80

You basically adapt the URL so it is a subdomain of your second server, then you strip it out again.
This part goes on the second (remote proxy server):

<ProxyMatch "http://.*\.[0-9]+\.server\.yourdomain\.com/.*">
    RewriteEngine on
    RewriteRule (proxy:http[s]?://.+)\.[0-9]+\.server\.premiumize\.me(.+) $1$2 
    ... your code ...
</ProxyMatch>
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
The Surrican
  • 29,118
  • 24
  • 122
  • 168
1

Try this:

# Prevents Apache from functioning as a forward proxy server (where you don't want)
ProxyRequests Off
# Preserve Host in http protocol on destination server
ProxyPreserveHost On
<Proxy *>
   Order deny,allow
   Allow from all
</Proxy>
# enable rewrite engine
RewriteEngine On 
# check header
RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
# execute forward proxy
RewriteRule (.*) http://server1/$1 [P,L,QSA]

# check header
RewriteCond %{HTTP:X-CUSTOM-HEADER} 2
# execute forward proxy
RewriteRule (.*) http://server2/$1 [P,L,QSA]
freedev
  • 25,946
  • 8
  • 108
  • 125
0

you should be able to achieve it by using the RewriteCond directive verifying with %{HTTP:header}.

Try the following:

RewriteEngine On 

RewriteCond %{HTTP:X-CUSTOM-HEADER} 1
RewriteRule (.*) http://p1.example.com$1 [P] 
ProxyPassReverse / http://p1.example.com

RewriteCond %{HTTP:X-CUSTOM-HEADER} 2
RewriteRule (.*) http://p2.example.com$1 [P] 
ProxyPassReverse / http://p2.example.com

Hope it helps. :)

szemian
  • 2,381
  • 3
  • 18
  • 22
  • this is elegant but the questioner claerly states that he is in need of a forward proxy requset. so the servers p1 and p2 should deliver the resources from an external 3rd party source, not from them directly. – The Surrican Sep 04 '11 at 12:34