green69
green69

Reputation: 1720

URLs redirection problems deploying a working django app in apache (via mod_wsgi)

I've a django app that works perfectly under the django development server. I'm trying to deploying it in apache2.2 using the mod_wsgi and I have errors.
In the httpd.conf file I "mounted" my app under the root /myapp using:

WSGIScriptAlias /myapp /home/path_to_my_app/apache_conf/django.wsgi


I've followed the standard indication to prepare django.wsgi. Now, I can reach the main page template of my app but it seems to have redirection errors. The "/myapp" root is not inserted automatically on ULRs redirection requests. Request from the main page template to some modules in views.py via urls.py cannot be correctly delivered.

apache error log:

127.0.0.1 - - [21/Feb/2011:16:11:44 +0100] "GET /myapp/ HTTP/1.1" 200 1795  
127.0.0.1 - - [21/Feb/2011:16:11:46 +0100] "GET /api/dir HTTP/1.1" 404 205

"/api/dir" doesn't exist, it should be matched from a pattern in urls.py, but id doesn't

I've spent more than 2 days with it, please can somebody help?

Upvotes: 1

Views: 2369

Answers (3)

Luis Masuelli
Luis Masuelli

Reputation: 12343

Actually it is a bit more complicated than just that.

There are some times when you need absolute in-app urls, because you don't want to reach an url which is relative to the current url.

e.g. you have a global menu in a main layout template (i.e. a template which is "extend"ed by each other template in your project): you cannot have relative urls like "home", "contact", "about", "blog" because if you are in /yourapp/blog/entries/2014/02/06/add (let's say such url exist) and you click any menu item, you would go to (say, for "home") /yourapp/blog/entries/2014/02/06/add/home instead to /home, which you would expect.

The ugly solution of virtual hosts is good here (in Java, Struts 2 has an option to resolve urls for "actions" (i.e. named urls / entry points) including the deploy context directory as deployed in the web container (e.g. tomcat)) ... don't know it there's any "context" setting you can guess (perhaps it's possible to return the current "WSGIScriptAlias" key as an environment variable). After that, using back the initial slash for urls ("/home").

Upvotes: 0

green69
green69

Reputation: 1720

Finally I found the error. It was not connected to httpd.conf file but to how URLs are specified both to django urls.py file and to templates.
As I mounted myapp in this way:

WSGIScriptAlias /myapp my_path_to_wsgi_module/django.wsgi

I was believing that URLs specified in django template files should carry an initial slash, like this: '/api/dir'
It results that in this way the application works only on django development server but not on apache.

Instead if you use URLs without initial slash like:
'api/dir'
The app works correctly both on django development server and on apache!

You must avoid using starting slashes even on pattern matching of django urls.py file:
like this: (r'^api/dir$', 'available_services')
and NOT like this: (r'^/api/dir$', 'available_services')

Maybe this is an obvious thing for expert django users but if you're novice like me, this can make you loose a certain amount of time because it's a hard problem to be detected.

Upvotes: 1

mrfunyon
mrfunyon

Reputation: 99

It appears you are missing some things from your apache config:

In the working config for my server I have:

# tell apache where to find the wsgi script.
WSGIScriptAlias / "/home/path_to_my_app/wsgi/my.settings.wsgi"
# turn on auto-reload in WSGI
WSGIScriptReloading On
WSGIReloadMechanism Process

# assign a process to a process group
WSGIDaemonProcess djangoapps processes=10 threads=1 maximum-requests=500 display-name=my-wsgi
WSGIProcessGroup djangoapps

Your WSGIScriptAlias line is correct but you need to tell wsgi how you are going to run your application. You are most likely getting 404's because there is not a WSGIDaemonProcess line to tell the WSGI handler how to work with your process.

http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives might be a helpful reference.

Upvotes: 1

Related Questions