【问题标题】:apache + wsgi + django : Client denied by server configurationapache + wsgi + django:客户端被服务器配置拒绝
【发布时间】:2015-10-22 20:26:54
【问题描述】:

我的 django 项目出现 403 错误。

我在错误日志中得到的唯一一行是:

AH01630: client denied by server configuration: /srv/http/www/src/myproject/preprod_wsgi.py

这个文件的权限是:

-rwxr-xr-x 1 root root  552 Jul 30 04:24 preprod_wsgi.py

并且 apache (VERSION : 2.4) conf 文件包含(除其他外):

LoadModule wsgi_module modules/mod_wsgi.so

<IfModule unixd_module>
    User http
    Group http
</IfModule>

<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Others
#

Alias /robots.txt /srv/http/www/htdocs/static/robots.txt
Alias /favicon.ico /srv/http/www/htdocs/static/favicon.ico


Alias /media/ /srv/http/www/htdocs/media/
Alias /static/ /srv/http/www/htdocs/static/


<Directory "/srv/http/www/htdocs/static">
           Require all granted
</Directory>

<Directory "/srv/http/www/htdocs/media">
           Require all granted
</Directory>

<Files "/srv/http/www/src/myproject/preprod_wsgi.py">
       Require all granted
</Files>

#
# WSGI
#

WSGIDaemonProcess myproject python-path=/srv/http/www/src
WSGIScriptAlias / /srv/http/www/src/myproject/preprod_wsgi.py
WSGIPythonPath /srv/http/www/src

DocumentRoot "/srv/http/www/htdocs"
<Directory "/srv/http/www/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

ErrorLog "/srv/http/www/logs/error_log"
CustomLog "/srv/http/www/logs/access_log" common

LogLevel warn

这也是我的 preprod_wsgi.py 文件的内容:

"""
WSGI config for soisbault project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os, sys

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.preprod_settings")

application = get_wsgi_application()

ALLOWED_HOSTS 也在我的preprod_settings.py 中设置。

我尝试使用&lt;Files&gt; 指令添加对该文件的访问权限,并对其进行了chmod 755。但我想不出我还能做些什么。

我该如何解决这个问题?如有必要,请随时询问更多详细信息。

提前谢谢你。

【问题讨论】:

  • 为什么文件(可能是项目的其余部分)归 root 所有?
  • @DanielRoseman 好问题。整个项目都在一个 docker 容器中,我猜这个项目的所有权是在容器构建过程中设置的。但是我运行了chown -Rv http 并重新启动了服务,但仍然出现此错误。
  • @DanielRoseman(反正有755权限,即使是root拥有,httpd不应该能够执行wsgi.py脚本吗?)
  • 你确定你在你的 django 设置文件中设置了ALLOWED_HOSTS = ['yourhost.com'],否则如果关闭调试会抛出 403。

标签: python django apache mod-wsgi http-status-code-403


【解决方案1】:

似乎是 apache 中的 指令不能在 指令之外使用。

<Files "/srv/http/www/src/myproject/preprod_wsgi.py">
       Require all granted
</Files>

我把它改成了

<Directory "/srv/http/www/src/myproject">
       <Files "preprod_wsgi.py">
              Require all granted
       </Files>
</Directory>

它奏效了。 :)

【讨论】: