Every url works fine in both Apache and django runserver except for urls (pointing to views, not real files) like this:
foo.js
urls.py:
url(r'^foo.js$', 'myapp.views.foo', name="foo"),
views.py:
def foo(request):
return HttpResponse("foo")
In development server calling this url outputs me foo
In Apache with mod_wsgi, with the same url i get 404
wsgi conf file (really basic):
import os
import sys, logging
sys.stdout = sys.stderr
path = '/var/www/vhosts'
if path not in sys.path:
sys.path.insert(0, path)
sys.path.insert(0, path + '/myapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Classic urls like /foo/ work as expected.
Where is the problem? Is it an apache behaviour that i don’t know / recognize? Is it Django routing?
edit:
After adding apache conf (thanks for the maybe obvious hint), i think that those AliasMatch on *.js are the problem.. i completely forgot them!
<VirtualHost *:80>
DocumentRoot "/var/www/vhosts/myapp"
ServerName www.myapp.local
Alias /robots.txt /var/www/vhosts/myapp/public/static/robots.txt
Alias /favicon.ico /var/www/vhosts/myapp/public/static/favicon.ico
AliasMatch ^/([^/]*\.css) /var/www/vhosts/myapp/public/static/$1
AliasMatch ^/([^/]*\.js) /var/www/vhosts/myapp/public/static/$1
Alias /media/ /var/www/vhosts/myapp/public/media/
Alias /static/ /var/www/vhosts/myapp/public/static/
<Directory /var/www/vhosts/myapp>
Order allow,deny
Allow from all
</Directory>
WSGIDaemonProcess myapp.djangoserver processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup myapp.djangoserver
WSGIScriptAlias / /var/www/vhosts/myapp/apache/django.wsgi
LogLevel info
</VirtualHost>
Did you set up Apache to serve the directories set as
MEDIA_ROOTandSTATIC_ROOTatMEDIA_URLandSTATIC_URL, respectively? In development, Django serves the static media for you so you don’t need a full webserver stack, but in production, it’s up to you to serve the files.UPDATE
Just remove the
AliasMatchlines. They’re unnecessary if you’ve already set upAliasdirectives for both /media/ and /static/. All static resources will be in one of those two directories.Also, don’t forget to run
python manage.py collectstatic. Otherwise, the directory specified asSTATIC_ROOTwill be empty, which would obviously also result in 404s.