Saturday, January 29, 2011

How to configure apache as forward proxy server to regex-replace domain names?

For example I want to set up a forward proxy to forward HTTP requests to a.com, b.com, c.com to a.mirror.com, b.mirror.com, c.mirror.com.

Currently I have to configure 3 vhosts as:

host-a: 
  ServerName a.com
  ProxyPass / http://a.mirror.com/
host-b: 
  ServerName b.com
  ProxyPass / http://b.mirror.com/
host-c: 
  ServerName c.com
  ProxyPass / http://c.mirror.com/

Is there any way to rewrite the domain part of http request? as:

ProxyPassMatch http://(.*).com/ http://$1.mirror.com/

I wonder if I must do it by RewriteRules, but I don't know how to write the rule exactly and the performance of RewriteRule v. ProxyPass, but performance isn't a big problem.

  • Depending on the details of your exact setup, this can be done using mod_rewrite. You would probably match on HTTP_HOST, strip out the part of the hostname that you want, and tack it onto .mirror.com, then use mod_rewrite's [P] flag to enable the proxy.

    This isn't exactly what you want, but it might get you a little closer:

    http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html#uservhosts

    From muffinista
  • You want something like this:

    RewriteCond %{HTTP_HOST} ^(.*)\.com
    RewriteRule (.*) %{SERVER_PROTOCOL}://%1.mirror.com$1 [P]
    

    (Untested.)

0 comments:

Post a Comment