我有一个具有以下目录结构的Django应用程序
/myapp/
/login/
/myapp_settings/
/subapp1/
/supapp2/
manage.py
位于myapp
目录中。
在项目url.py
中,我有以下网址设置:
urlpatterns = patterns('',
url(r'^subapp1/', include('subapp1.urls')),
url(r'^xhr/', include('subapp1.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^subapp2/', include('smart_selects.urls')),
# Login / logout.
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page': '/subapp1/'}, name='auth_logout'),
url(r'^logout/(?P<next_page>.*)/$', 'django.contrib.auth.views.logout', name='auth_logout_next'),
)
在开发runserver
上部署时,所有内容都会正确链接和加载。当我将整个myapp
目录部署到Apache上的Django根目录时,我发现它没有按预期链接。
例如,如果我链接到example.com/login/
,我会得到一个Apache 404.我认为这是因为我没有为该特定目录定义虚拟目录配置。
我在Apache中为我的应用程序设置了以下内容:
WSGIScriptAlias /myapp /var/www/django-projects/myapp/myapp_settings/wsgi.py
WSGIPythonPath /var/www/django-projects/myapp
Alias /media/ /var/www/django-projects/myapp/media/
Alias /static/ /var/www/django-projects/myapp/static/
<Directory /var/www/django-projects/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/django-projects/myapp/media>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/django-projects/myapp/myapp_settings/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
我是否需要为我使用的每种网址格式提供别名和目录配置?如果是这样,我如何重做我的网址格式,以便我不需要这样做。在部署应用程序时,我不希望必须执行所有这些额外的Apache配置。
编辑:我根据Reinbach的建议修改了WSGIScriptAlias
。它现在显示为WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py
。但是,这仍然返回404.Apache日志中的错误说
[Fri Sep 07 09:11:00 2012] [error] [client 192.189.x.x] File does not exist: /var/www/html/login
请注意,它正在查看/var/www/html
(默认Apache root)而不是/var/www/django-projects
EDIT2:我正在为此部分附加VirtualHost广告块
WSGIPythonPath /var/www/django-projects/myapp
<VirtualHost sub.example.com:80>
DocumentRoot /var/www/django-projects/myapp
ServerName sub.example.com
WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py
Alias /robots.txt /var/www/django-projects/myapp/static/robots.txt
Alias /favicon.ico /var/www/django-projects/myapp/static/favicon.ico
AliasMatch ^/([^/]*\.css) /var/www/django-projects/myapp/static/css/$1
AliasMatch ^/([^/]*\.js) /var/www/django-projects/myapp/static/js/$1
AliasMatch ^/([^/]*\.png) /var/www/django-projects/myapp/static/images/$1
AliasMatch ^/([^/]*\.swf) /var/www/django-projects/myapp/static/swf/$1
Alias /media/ /var/www/django-projects/myapp/media/
Alias /static/ /var/www/django-projects/myapp/static/
<Directory /var/www/django-projects/myapp/static>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/django-projects/myapp/media>
Order deny,allow
Allow from all
</Directory>
<Directory /var/www/django-projects/myapp/myapp_settings/>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</VirtualHost>
答案 0 :(得分:3)
我相信您希望更改您的WSGIScriptAlias,因为当您的示例显示您尝试使用example.com/myapp/login
时,您希望处理example.com/login
WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py
答案 1 :(得分:1)
长篇短篇
自Apache 2.4.x以来,不赞成使用Allow和Deny,请改为使用Require all(或拒绝)。
长篇大论
我在尝试使用Apache和mod_wsgi设置Django时遇到了同样的问题。我不完全确定为什么会发生这种情况,但是当我在httpd.conf
中注释掉以下几行时# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
#<Directory />
# AllowOverride none
# Require all denied
#</Directory>
一切正常。
我不确定这是否是解决问题的正确而安全的方法,但我希望它可能有所帮助。
PS 我认为不应该引起很多安全问题,因为根目录无论如何都是别名:WSGIScriptAlias / /var/www/django-projects/myapp/myapp_settings/wsgi.py
,但我可能会知道。
P.P.S。看起来更好的解决方案是将这些行取消注释,但更改Order和Allow指令以支持Require all grant。例如:
<Directory /var/www/django-projects/myapp/myapp_settings/>
<Files wsgi.py>
Require all granted
#Order deny,allow
#Allow from all
</Files>
</Directory>
然而,这只是一个试验和错误的解决方案,我可以提出来。我没有深入理解为什么它有效但订单和允许没有。
P.P.P.S 哦,现在我知道了。自Apache 2.4.x以来,不允许使用Allow和Deny。可以找到好的答案here。