2

I am trying to configure Apache to host a Django webservice and a PHP website.

All URLs with the pattern www.mysite.com/api should be directed to the Django service. All other URLs (e.g. www.mysite.com) should be directed to the PHP website.

My virtual host config looks like this:

...
WSGIScriptAlias /api /var/www/mysite.com/apache/django.wsgi
<Directory /var/www/mysite.com/apache>
    Order allow,deny
    Allow from all
</Directory>

Alias / /var/www/mysite.com/apache/php/
<Directory /var/www/mysite.com/apache/php>
    Options Indexes FollowSymLinks
    AllowOverride All
    Order Deny,Allow
    Allow from all
</Directory>
...

This directs each request to the correct handler. However, my Django urls.py looks like this:

...
api_patterns = patterns('',
    url(r'^api/1.0/$', views.api_root),
    url(r'^api/1.0/oauth/', 
        include('oauth2_provider.urls', 
                namespace='oauth2_provider')),
...

This fails to match any URLs because it is expecting them to begin with api. This prefix is, of course, being stripped off before it gets here (when the requests arrive here they are in the format 1.0/oauth etc.).

Is there any way to pass a fully intact URL through to Django?

Solution

Graham Dumpleton, the author of mod_wsgi, suggested a couple of approaches to fix this, both of which worked (see the accepted answer below).

I went for the solution he suggests in UPDATE 1. Appending /api to the file path solves the problem:

WSGIScriptAlias /api /var/www/mysite.com/apache/django.wsgi/api

i.e. /api is no longer stripped off the URL by the time it reaches Django.

So here are the relevant parts of my working config:

...
DocumentRoot /var/www/mysite.com/apache/php

WSGIScriptAlias /api /var/www/mysite.com/apache/service/django.wsgi/api
<Directory /var/www/mysite.com/apache/service>
    WSGIProcessGroup www.mysite.com
    WSGIPassAuthorization On
    Options -Indexes
    Order allow,deny
    Allow from all
</Directory>

# PHP web site
<Directory /var/www/mysite.com/apache/php>
    Options -Indexes FollowSymLinks
    AllowOverride All
    Order Deny,Allow
    Allow from all
    DirectoryIndex index.php
</Directory>
...

1 Answer 1

5

What you are seeing is exactly how it is generally meant to work. A WSGI application should not care nor embed knowledge about where it is being mounted. In other words, you should not have 'api' as a prefix in urls.py. Why do you believe you need 'api' prefix in urls.py?


UPDATE 1

You may be able to use:

WSGIScriptAlias /api /var/www/mysite.com/apache/django.wsgi/api

I can't remember the exact formula right now and whether that is correct, but try it.

6
  • Well, my Django app is not called api. I would like to use the same URL patterns in the dev server and Apache. Nov 19, 2013 at 11:14
  • I guess neither of those are particularly good reasons... Nov 19, 2013 at 11:27
  • That is a deficiency of the development server, although other pure Python WSGI servers tend not to be any better. See possible workaround as update to answer. Nov 19, 2013 at 11:38
  • Your workaround worked - thanks very much! I had to modify my approach slightly - I have added it as an edit to the original question. Thanks again! Nov 19, 2013 at 17:26
  • You should not have been using Alias with '/' to start with. Use DocumentRoot instead. Nov 20, 2013 at 9:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .