Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have webserver1 behind a router currently serving all http traffic to mydomain.com. I just added webserver2, and want to redirect mydomain.com/server2 traffic to that box. To the user, the redirect should be unnoticed (i.e. the URL should just be mydomain.com/server2, and the redirection happens behind the scenes). How do I set this up in the apache configuration of webserver1 (I'm assuming webserver2's config needs to do nothing special)?

I've tried the advice given here, using mod_rewrite, but it didn't seem to do the trick:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/server2/
RewriteRule ^/$ http://192.168.1.102/ [P,L]

In case it is relevant, webserver1 is hosting a django app using mod_wsgi, with a few other apps that get redirected away. Here is the virtualhost conf:

<VirtualHost *:80>
    ServerAdmin admin@mydomain.com
    ServerName  www.mydomain.com
    ServerAlias 127.0.0.1

    RewriteEngine on
        RewriteCond %{REQUEST_URI} ^/server2/
        RewriteRule ^/$ http://192.168.1.102 [P,L]

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

    <Directory /var/www/mydomain>
    Order allow,deny
    Allow from all
    </Directory>

    ...

    WSGIDaemonProcess mydomain user=user group=user threads=25
    WSGIProcessGroup mydomain

    WSGIScriptAlias / /home/user/mydomain/apache/django.wsgi
    Alias /phpmyadmin /usr/share/phpmyadmin/

</VirtualHost>

Thanks in advance.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Mod_Rewrite is more flexible than mod_proxy. Uncomment the load line for it.

Simple comparison here http://www.wellho.net/mouth/1376_Choosing-between-mod-proxy-and-mod-rewrite.html

<VirtualHost *:80>

RewriteEngine on

# just in case (don't want to accidentally expose all the internal servers) !
ProxyRequests off

# define a log file
RewriteLog /var/log/apache/server2.rewrite.log
RewriteLogLevel 1

# add the tailing / if not there
RewriteRule     ^/server2$          http://www.mydomain.com/server2/  [R] [L]

# proxy the request to internal url
RewriteRule     ^/server2/(.*)      http://192.168.1.102/$1 [P]

Note that this example is case sensitive.

share|improve this answer
add comment

Use:

ProxyPass /server2 http://192.168.1.102/server2

You may also need ProxyPassReverse as well. See Apache documentation:

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass

Note that backend mod_wsgi MUST mount application at same sub URL as it is appearing and being proxied as on front end.

Also be aware that may require configuration on back end to fiddle what host/port it appears back end application is running on so URL reconstruction in back end works properly when used. So research that if you find it is an issue.

share|improve this answer
    
I am getting a 403 forbidden error and this is in webserver1's apache error log: [Sun Sep 05 18:21:13 2010] [error] [client xxx.xxx.xxx.xxx] client denied by server configuration: proxy:192.168.1.102 Do I need mod_proxy on webserver2? Also, I'm not clear on what you meant by the mounting issue with mod_wsgi. The django app on webserver1 is mounted at /, but it is served at mydomain.com/app –  NP01 Sep 5 '10 at 12:53
    
Would have no idea why you get 403 as you haven't supplied any details about front end configuration. As to Django application mounting, you will need for first argument to WSGIScriptAlias to be /server2. ie., match what sub URL it appears as on front end. If you don't do this, you will need to use WSGI middleware wrapper to fiddle SCRIPT_NAME to be /server2. Don't do this and you will find that redirects issues from Django application will be wrong as will be missing /server2 in the redirect URL. –  Graham Dumpleton Sep 5 '10 at 23:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.