调试 Apache/Django/WSGI 错误请求 (400) 错误 [英] Debugging Apache/Django/WSGI Bad Request (400) Error

查看:29
本文介绍了调试 Apache/Django/WSGI 错误请求 (400) 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的简单 Django 应用程序在调试模式下运行良好 (manage.py runserver),并且在我的开发箱上的 WSGI+Apache 下运行,但是当我推送到 EC2 时,我开始收到间歇性的(10-80% 的情况下)我尝试查看的任何 URL 的 Bad Request (400) 错误(无论是在我的应用程序中还是在 Django 管理员中.

My simple Django app worked fine in debug mode (manage.py runserver), and works under WSGI+Apache on my dev box, but when I pushed to EC2 I began receiving intermittent (10-80% of the time) errors of Bad Request (400) for any URLs I try to view (whether in my app or in the Django admin.

在哪里可以找到有关此问题的调试信息?/var/log/apache2/error.log 中没有任何内容,即使是 LogLevel=info.我检查了版本,记录了请求环境(参见 ModWSGI 调试技巧)并且没有看到任何主要的差异.

Where can I find debug information about this? Nothing appears in /var/log/apache2/error.log, even with LogLevel=info. I have checked versions, logged the Request environment (cf. ModWSGI Debugging Tips) and see no major differences.

我剩下的一个想法是,我正在使用 Ubuntu 12.04 (libapache2-mod-wsgi 3.3-4build1) 中的 mod_wsgi,它是针对 Python 2.7.1 构建的;我有 Python 2.7.3.而 Django 是 1.6,比 Ubuntu Precise 版本更新.我犹豫是否开始从源代码构建包,因为它很难清理,而且这些看起来像是次要的版本更改...

The one remaining thought I had is, I'm using the mod_wsgi from Ubuntu 12.04 (libapache2-mod-wsgi 3.3-4build1) which was built against Python 2.7.1; I have Python 2.7.3. And Django is 1.6, which is newer than the Ubuntu Precise version. I hesitate to start building packages from source since it's so hard to clean up and these seem like minor version changes...

感谢您的帮助.

(作为参考,这里是 Apache 配置和 WSGI 应用程序)

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www
    WSGIScriptAlias /rz /usr/local/share/rz/rz.wsgi
    ...

rz.WSGI 应用

import os
import sys
import django.core.handlers.wsgi
import pprint

path = '/usr/local/share/rz'
if path not in sys.path:
    sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'rz.settings'

class LoggingMiddleware:
    def __init__(self, application):
        self.__application = application

    def __call__(self, environ, start_response):
        errors = environ['wsgi.errors']
        pprint.pprint(('REQUEST', environ), stream=errors)

        def _start_response(status, headers, *args):
            pprint.pprint(('RESPONSE', status, headers), stream=errors)
            return start_response(status, headers, *args)

        return self.__application(environ, _start_response)

application = LoggingMiddleware(django.core.handlers.wsgi.WSGIHandler())

推荐答案

像这样将 ALLOWED_HOSTS 设置添加到 settings.py 中...

Add the ALLOWED_HOSTS setting to your settings.py like so...

ALLOWED_HOSTS = [
    '.example.com', # Allow domain and subdomains
    '.example.com.', # Also allow FQDN and subdomains
]

我遇到了同样的问题,并在文档中找到了答案这里

I had this same problem and found the answer here in the docs

更新:django 1.6 文档不再在线,我更新了链接以转到 ALLOWED_HOSTS 设置的 django 1.7 文档.

update: django 1.6 docs are no longer online, I updated the link to go to the django 1.7 docs for ALLOWED_HOSTS setting.

这篇关于调试 Apache/Django/WSGI 错误请求 (400) 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆