Python 调试 Apache/Django/WSGI 错误请求 (400) 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20321673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging Apache/Django/WSGI Bad Request (400) Error
提问by mrisher
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.
我的简单 Django 应用程序在调试模式 ( manage.py runserver)下运行良好,并且在我的开发箱上在 WSGI+Apache 下运行,但是当我推送到 EC2 时,我开始收到间歇性(10-80% 的时间)错误的Bad Request (400)任何 URL 我尝试视图(无论是在我的应用程序中还是在 Django 管理员中。
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.
我在哪里可以找到有关此的调试信息?什么都没有出现/var/log/apache2/error.log,即使是LogLevel=info. 我已经检查了版本,记录了请求环境(参见ModWSGI 调试技巧)并且没有发现重大差异。
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...
我剩下的一个想法是,我正在使用 Ubuntu 12.04 (libapache2-mod-wsgi 3.3-4build1) 中的 mod_wsgi,它是针对 Python 2.7.1 构建的;我有 Python 2.7.3。而 Django 是 1.6,比 Ubuntu Precise 版本更新。我犹豫是否开始从源代码构建包,因为它很难清理,而且这些看起来像是次要的版本更改......
Thank you for your help.
感谢您的帮助。
(For reference, here are the Apache config and WSGI apps)
(作为参考,这里是 Apache 配置和 WSGI 应用程序)
Apache config (000-default)
Apache 配置(000-默认)
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
WSGIScriptAlias /rz /usr/local/share/rz/rz.wsgi
...
rz.WSGI app
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())
采纳答案by teewuane
Add the ALLOWED_HOSTS setting to your settings.py like so...
像这样将 ALLOWED_HOSTS 设置添加到 settings.py 中...
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
我遇到了同样的问题,并在文档中找到了答案
update: django 1.6 docs are no longer online, I updated the link to go to the django 1.7 docs for ALLOWED_HOSTSsetting.
更新:django 1.6 文档不再在线,我更新了链接以转到 django 1.7 文档进行ALLOWED_HOSTS设置。
回答by Yuji 'Tomita' Tomita
If you've definitely set ALOWED_HOSTS- make sure your hostname doesn't contain underscores. It's technically illegal.
如果您已确定设置ALOWED_HOSTS- 确保您的主机名不包含下划线。这在技术上是非法的。
I had to print out various functions and it boiled down to this regex failing to detect a domain in django.http
我不得不打印出各种函数,归结为这个正则表达式无法检测到域 django.http
host_validation_re = re.compile(r"^([a-z0-9.-]+|\[[a-f0-9]*:[a-f0-9:]+\])(:\d+)?$")
And indeed, my domain had an underscore in it.
事实上,我的域中有一个下划线。
回答by user3770635
This is not a solution, but for debugging purposesyou might set the ALLOWED_HOSTS setting in your settings.py like this
这不是解决方案,但出于调试目的,您可以像这样在 settings.py 中设置 ALLOWED_HOSTS 设置
ALLOWED_HOSTS = ['*']
It should definitely work. If not, at least you will know the problem isn't Django denying access to the given url.
它绝对应该工作。如果没有,至少你会知道问题不是 Django 拒绝访问给定的 url。

