Forum Moderators: phranque

Message Too Old, No Replies

Simple mod rewrite proxy setup

mod_rewrite proxy to local webserver on different port

         

fishwebby

9:29 am on Jan 14, 2008 (gmt 0)

10+ Year Member



Hello,

I'm having a small problem setting up my PC with an Apache proxy. I'm running Apache 2 on port 80, but additionally I have another local web server (used for Rails development) running on port 3000. I have several websites that I use in Apache on port 80, for example [localhost...] I can access my other server like this: [localhost:3000...] but there are issues with it not running on port 80. So, what I have done is to set up an entry in my hosts file like this:


127.0.0.1 dev.example.com

and then a virtual host like this:


<VirtualHost dev.example.com>
ServerName dev.example.com
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyPreserveHost On
</VirtualHost>

Which allows me to navigate to [dev.example.com...] and it serves content internally from [localhost:3000...] All well and good. However... doing this means I can't access the other apps I have in Apache (e.g. [localhost...] no longer works).

So, what I've read might be possible is to use mod_rewrite as a proxy instead, hopefully leaving intact my existing apps in Apache and only proxying requests to dev.example.com.

This is the rewrite code that I've tried, but alas it doesn't work:


RewriteEngine on
RewriteRule ^dev.example.com/* http://localhost:3000/$1 [P]

I would be extremely grateful if anyone could tell me if what I'm attempting is possible, and if so what is wrong with my rewrite code (I'm still a beginner as far as mod_rewrite is concerned!).

Thanks in advance!
Dave

gergoe

1:08 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



You only need a new VirtualHost, which will serve all your other websites. To do this, you have to make this new virtualhost as the default one, which only means you have to put before the one you already defined, something like this:

NameVirtualHost 127.0.0.1 
#
# This one will answer to requests to all request except dev.example.com.
# This is the first VirtualHost (for a given NameVirtualHost group), thus
# any request whose Host header does not match any of the other VirtualHosts
# will end up here.
<VirtualHost 127.0.0.1>
ServerName default.example.com
DocumentRoot path/to/your/webroot
</VirtualHost>
#
# This VirtualHost will answer request for dev.example.com only
<VirtualHost 127.0.0.1>
ServerName dev.example.com
ProxyPass / [localhost:3000...]
ProxyPassReverse / [localhost:3000...]
ProxyPreserveHost On
</VirtualHost>

Please note that the host (and port) defined in NameVirtualHost must exactly match the one defined in the VirtualHosts, otherwise the request to VirtualHost mapping will not work properly.

[edit]Additionally whatever you put into the NameVirtualHost directive, it will not affect your Listen/Bind/Port directives, so if you define

NameVirtualHost 192.168.0.1
, you must have a corresponding
Listen 192.168.0.1
in your Apache configuration file.[/edit]

fishwebby

2:46 pm on Jan 14, 2008 (gmt 0)

10+ Year Member



Hi gergoe,

I've managed to get it working now, based on your instructions - thanks very much for such a detailed reply! :-)

Best wishes,
Dave