每个 url 在 Apache 和 django runserver 中都可以正常工作,除了 url(指向视图,而不是真实文件),如下所示:
foo.js
网址.py:
url(r'^foo.js$', 'myapp.views.foo', name="foo"),
视图.py:
def foo(request):
return HttpResponse("foo")
在开发服务器中调用此 url 输出我foo
在带有 mod_wsgi 的Apache中,我得到的 url 相同404
wsgi conf 文件(非常基础):
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()
经典的 url 像/foo/
预期的那样工作。
问题出在哪里?这是我不知道/不认识的 apache 行为吗?是 Django 路由吗?
编辑:
添加 apache conf 后(感谢可能显而易见的提示),我认为 *.js 上的 AliasMatch 是问题所在。我完全忘记了它们!
<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>