Apache + mod_wsgi + django Webfaction configuration issue
A problem that I stumbled upon recently, and although I solved it, would love to hear your opinion on the correct / simple / accepted solution.
I am developing a site using Django + python. When I run it on my local machine with "python manage.py runningserver", the default local address is http://127.0.0.1:8000/ .
However, on the production server, my application has a different url, with a path similar to http://server.name/myproj/ "
I need to create and use persistent urls. If I use {% url view params%} I get paths related to / since my urls.py contains this
urlpatterns = patterns('',
(r'^(\d+)?$', 'myproj.myapp.views.index'),
(r'^img/(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT + '/img' }),
(r'^css/(.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT + '/css' }),
)
So far, I see 2 solutions:
- modify urls.py, include '/ myproj /' in case of production run
- use
request.build_absolute_uri()
to create link in views.py or pass some variable named "hostname: port / path" in templates
Are there any nicer ways to solve this problem? Thanks.
Refresh . Well, the problem doesn't seem to be with django, but with the webfaction way of setting up wsgi. The Apache configuration for the application with the URL "hostname.com/myapp" contains the following line
WSGIScriptAlias / /home/dreamiurg/webapps/pinfont/myproject.wsgi
So SCRIPT_NAME is empty and the only solution I can see is to switch to mod_python or serve my application from root. Any ideas?
a source to share
You don't have to do anything special. Django honors the environment variable SCRIPT_NAME
that is set by mod_wsgi when you serve a non-root Django site and automatically adds it to your URL reversal code.
If you are using mod_python (you shouldn't be), you may need to set django.root
Apache in your config.
Updated I suspect this has to do with the way Webfaction serves Django sites through an Apache proxy instance - this instance does not know the actual mount point as defined by the Webfaction dashboard.
In this case, you will probably need to install SCRIPT_NAME
manually in your .wsgi
script. I think this should work:
_application = django.core.handlers.wsgi.WSGIHandler()
def application(environ, start_response):
os.environ['SCRIPT_NAME'] = '/myproj/'
return _application(environ, start_response)
a source to share
Edit:
WSGIScriptAlias / /home/dreamiurg/webapps/pinfont/myproject.wsgi
at
WSGIScriptAlias /myproj /home/dreamiurg/webapps/pinfont/myproject.wsgi
Then change the nginx WebFaction front end config to proxy to '/ myproj' on the back instead of '/'.
That should be all that is required. You must not use the '/ myproj' prefix in urls.py.
In other words, just make sure the mount point for the rear end is the same as where it is mounted on the front.
Modify your WSGI script file to reassign SCRIPT_NAME, although it may work, it is generally not recommended as it prevents Apache / mod_wsgi from doing the right thing, which could have other consequences.
a source to share