Django/Apache/mod_wsgi: How do I specify the SCRIPT_NAME for non-root URLs?

1,117 views
Skip to first unread message

Tyler Erickson

unread,
Jan 6, 2010, 2:39:05 PM1/6/10
to Django users
I am trying to use Apache and mod_wsgi to serve multiple django
sites. The django sites are served up fine when at the root URL, but
when served from a sub-URL the portion of the the URL consumed by
Apache disappears from any links created in the django site pages.
From reading throught the WSGI spec (http://www.wsgi.org/wsgi/
Specifications/routing_args), it appears that I need to specify the
SCRIPT_NAME environment variable, but I am not sure where I should do
that. (The apache/django.wsgi file seemed like a logical place to set
the SCRIPT_NAME environment variable, but that seems to have no effect
on the generated URLs.)

Below is a description of my setup, and the apache and WSGI
configuration files.

- Tyler


OS: Ubuntu 9.04
django: 1.1 SVN-11733
apache: 2.2.11-2ubuntu2.3
libapache2-mod-wsgi: 2.3-1build1


#############################################
Apache configuration file (default)
#############################################
<VirtualHost *:80>
ServerAdmin webmaster@localhost

###############################################################
# Configuration for the olwidget test project
###############################################################
Alias /olwidget/site_media/ "/usr/local/olwidget/trunk/olwidget/
django-olwidget/test_project/site_media/"
<Directory "/usr/local/olwidget/trunk/olwidget/django-olwidget/
test_project/site_media/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

Alias /olwidget/media/ "/usr/local/lib/python2.6/dist-packages/
django/contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/
admin/media/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

WSGIScriptAlias /olwidget /usr/local/olwidget/trunk/olwidget/
django-olwidget/test_project/apache/django.wsgi
<Directory /usr/local/olwidget/trunk/olwidget/django-olwidget/
test_project/apache>
Order deny,allow
Allow from all
</Directory>

###############################################################
# Configuration for WFEIS
###############################################################
Alias /wfeis/site_media/ "/usr/local/django_nasa_fire_dss/
site_media/"
<Directory "/usr/local/django_nasa_fire_dss/site_media/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

Alias /wfeis/media/ "/usr/local/lib/python2.6/dist-packages/django/
contrib/admin/media/"
<Directory "/usr/local/lib/python2.6/dist-packages/django/contrib/
admin/media/">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

WSGIScriptAlias /wfeis /usr/local/django_nasa_fire_dss/apache/
django.wsgi
<Directory /usr/local/django_nasa_fire_dss/apache>
Order deny,allow
Allow from all
</Directory>

</VirtualHost>


#############################################
Example WSGI configuration file
(/usr/local/olwidget/trunk/olwidget/django-olwidget/test_project/
apache/django.wsgi)
#############################################
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_project.settings'
os.environ['SCRIPT_NAME'] = 'olwidget'
# append the django project root directory to the python path
sys.path.append('/usr/local/olwidget/trunk/olwidget/django-olwidget')
sys.path.append('/usr/local/olwidget/trunk/olwidget/django-olwidget/
test_project')
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Karen Tracey

unread,
Jan 6, 2010, 9:43:20 PM1/6/10
to django...@googlegroups.com
On Wed, Jan 6, 2010 at 2:39 PM, Tyler Erickson <tyler...@gmail.com> wrote:
I am trying to use Apache and mod_wsgi to serve multiple django
sites.  The django sites are served up fine when at the root URL, but
when served from a sub-URL the portion of the the URL consumed by
Apache disappears from any links created in the django site pages.
From reading throught the WSGI spec (http://www.wsgi.org/wsgi/
Specifications/routing_args
), it appears that I need to specify the
SCRIPT_NAME environment variable, but I am not sure where I should do
that.  (The apache/django.wsgi file seemed like a logical place to set
the SCRIPT_NAME environment variable, but that seems to have no effect
on the generated URLs.)


It should Just Work, no need to set any environment variables.  There have been a couple of threads recently similar to this:

http://groups.google.com/group/django-users/browse_thread/thread/ce1436670a3c55d5/
http://groups.google.com/group/django-users/browse_thread/thread/e790664ee1855647/

The first identified a Django problem in a very specific case (reverses called during processing of requests for the project root url without a trailing slash).  The cause of the second I don't recall seeing an answer on. When I try it, it works for me, excepting the one case identified in the first thread.  There is a fair amount of debugging advice to be found in those threads, so perhaps they will help you in tracking down what is going wrong in your case.

Karen

Tyler Erickson

unread,
Jan 8, 2010, 9:24:11 AM1/8/10
to Django users
I added the WSGI logging middleware to write to the Apache log as
described in:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques#Tracking_Request_and_Response
and yes it appears that my SCRIPT_NAME environment variable is being
automatically set (see excerpts below). So I guess it is Django or my
Django project that is causing the problem...

- Tyler

[Fri Jan 08 14:15:16 2010] [error] [client 198.111.191.240]
'PATH_INFO': '',
[Fri Jan 08 14:15:16 2010] [error] [client 198.111.191.240]
'REQUEST_URI': '/olwidget',
[Fri Jan 08 14:15:16 2010] [error] [client 198.111.191.240]
'SCRIPT_NAME': '/olwidget',

[Fri Jan 08 14:14:14 2010] [error] [client 198.111.191.240]
'PATH_INFO': '/',
[Fri Jan 08 14:14:14 2010] [error] [client 198.111.191.240]
'REQUEST_URI': '/olwidget/',
[Fri Jan 08 14:14:14 2010] [error] [client 198.111.191.240]
'SCRIPT_NAME': '/olwidget',

[Fri Jan 08 14:16:27 2010] [error] [client 198.111.191.240]
'PATH_INFO': '/test/test',
[Fri Jan 08 14:16:27 2010] [error] [client 198.111.191.240]
'REQUEST_URI': '/olwidget/test/test',
[Fri Jan 08 14:16:27 2010] [error] [client 198.111.191.240]
'SCRIPT_NAME': '/olwidget',

Tyler Erickson

unread,
Jan 8, 2010, 3:05:21 PM1/8/10
to Django users
Fixed it.
It turned out to be a problem with how the django project templates
were written. Both of the projects that I was experimenting with were
using hardcorded href values, instead of using a url template tag.

hardcoded example:
<a href='/test/styleinfo/'>Per-geometry styles</a>

url template tag example:
<a href={% url testolwidget.views.style_infomodel %}>Per-geometry
styles</a>

reference:
http://docs.djangoproject.com/en/1.1/ref/templates/builtins/#url

Reply all
Reply to author
Forward
0 new messages