tags:

views:

55

answers:

2

I am trying to figure out a how to forward a specific alias of a server to a specific django web app, and at the same time, keep the URL address bar in the user's browser to only show the server alias.

To be more specific:

This is for an intranet project at my company. We have a big linux server which does a lot of computational work, and it is also running apache to serve a variety of web pages and apps. My current django app is running at:

http://deptserver.example.com/mydjangoapps/myapp

But, I would love it if my users could use this instead:

http://myapp.example.com/

(I already have the IT folks forwarding myapp.example.com to deptserver.example.com via a CNAME in the DNS)

I can't break anything on the apache server, since it is serving critical stuff, but I do have the ability to add in things like a VirtualHost or some url rewriting rules, etc.

This is my django setup in the apache httpd.conf file:

<Location "/mydjangoapps">
    SetHandler python-program
    PythonHandler django.core.handlers.modpython
    SetEnv DJANGO_SETTINGS_MODULE mydjangopath.settings
    PythonOption django.root /mydjangoapps
    PythonPath "['/home/me/mydjangopath'] + sys.path"
    PythonDebug On
</Location>

And, this is in the sites-enabled default file (i.e. I can't break this part of the server):

<VirtualHost *:80>
        ServerAdmin [email protected]
        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
</VirtualHost>

In that above VirtualHost I've tried something like this using mod_rerwite:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^myapp\.example\.com
RewriteRule ^(.*) mydjangoapps/myapp$1

Which doesn't work because it thinks that mydjangoapps/myapp is a file path and not a URL path, so it gives me a 400 error. I wish that it would forward to a URL path instead.

I have also tried this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^myapp\.example\.com
RewriteRule ^(.*) http://deptserver.example.com/mydjangoapps/myapp$1

Which just forwards the user to that long URL.

These are the things I have thought about but haven't tried too much: virtualhost mod_rewrite alias proxy (?) port (putting my own apache server on a different port of the main server)

What's the best way (or only way) to do this?

Thanks!

+2  A: 

Ask your IT folks to add another IP address (let's say a.b.c.d) to that box's network interface, and then route myapp.example.com to that new ip address.

That way, you can set up a completely separate VirtualHost:

<VirtualHost a.b.c.d:80>
    # Your configuration here
</VirtualHost>
<VirtualHost *:80>
    # The existing configuration here, unchanged
</VirtualHost>

Then you just have to change <Location "/mydjangoapps"> to <Location "/"> and you'll be able to access your website from a browser at http://myapp.example.com/.

AndrewF
If I did that, then do you think I could make the root URL path go directly to my app, or would it end up like this: http://myapp.example.com/myapp
brfox
Absolutely. That's how I have my sites set up. I added details to my answer.
AndrewF
Cool. But, the does the VirtualHost have to be an IP address, or can I just use the DNS alias: myapp.example.com which already points to this server? Or could I use a different port instead? Thanks for your input, I'll wait a while longer to see if anyone else has some ideas, too
brfox
+1  A: 

As AndrewF noted, VirtualHosts are the way to go here. If you don't want to use the IP address, you can just set it up with name-based virtual hosting:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName myapp.example.com
    (...etc...)

Then that bit of the configuration will only respond to requests on myapp.example.com, leaving the standard configuration to pick up everything else.

Edit after comment If you're on a Debian-based system, which you seem to be as you mention sites-enabled, you shouldn't really be putting anything in httpd.conf - all the site-specific stuff should be in a separate file. This file should go in sites-available, and then run sudo a2ensite my_site_file to symlink it to the sites-enabled directory.

Daniel Roseman
Does the order of the VirtualHost definitions matter, i.e. should I put my new one before the existing one? Then, do I stick all my django Location stuff inside the VirtualHost defn - right now the Location section is in httpd.conf. Thanks!
brfox
That worked perfectly! I tried something very similar to this before I posted (I named the server in the VirtualHost line instead of in the ServerName line). Now that I did *:80 and put the django Location stuff in the VirtualHost it works just as advertised. Thanks very much.
brfox