错误请求 400:nginx / gunicorn

43

我按照这个教程进行操作:http://blog.wercker.com/2013/11/25/django-16-part3.html,现在只是想用Vagrant在本地测试。我并不想使用Wercker。

安装完成后,我尝试访问网站,但每次都出现Bad Request (400)错误。我不知道是nginx还是gunicorn的问题。

它们都有一个日志条目,所以我至少知道请求已经通过gunicorn并没有在nginx层面被停止。

问题在哪里?Gunicorn?nginx?

这里是gunicorn和nginx的日志。

我发现缺少网站图标,但这不应该阻止页面显示,对吗?

Gunicorn:

 >>> cat /var/local/sites/hellocities/run/gunicorn.error.log
10.0.0.1 - - [28/Jan/2014:07:05:16] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

Nginx:

>>> cat /var/log/nginx/hellocities-access.log
10.0.0.1 - - [28/Jan/2014:07:05:16 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:05:20 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:44 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

>>> cat /var/log/nginx/hellocities-error.log
2014/01/28 07:05:20 [error] 13886#0: *1 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
2014/01/28 07:09:44 [error] 13886#0: *3 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"

24
你在settings.py文件中设置了ALLOWED_HOSTS吗?https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts - Kamil Rykowski
嗯,我想我是这样做的。我使用以下命令运行playbook:ansible-playbook site.yml --extra-vars "source_location=$REMOTE_SOURCE_DIR hostname=$HOSTNAME" -u root其中$HOSTNAME127.0.0.1。然后在教程中的脚本中,变量通过export DJANGO_ALLOWED_HOST={{hostname}}进行设置,而ALLOWED_HOSTS = [os.environ.get('DJANGO_ALLOWED_HOST', '127.0.0.1'),] - Michael
1
好的,你是对的,问题出在“DJANGO_ALLOWED_HOST”变量上。 我在设置文件中设置了“ALLOWED_HOSTS = ['*']”,然后它就可以工作了。 但是当我使用本地vagrant时,我不知道应该放什么值……?“127.0.0.1”不起作用…… 非常感谢。 - Michael
2
在域名中使用下划线也会触发此错误。这不适用于Django 1.3和1.4,但至少适用于1.6.1。请参见https://code.djangoproject.com/ticket/20264,以了解为什么这种行为是正确的。 - Rune Kaagaard
3个回答

88

1
ALLOWED_HOSTS = ("*",) 和启动 Apache(service apache2 restart) - Halil
4
请注意,问题指定了gunicorn/nginx,而不是Apache2。个人建议避免使用ALLOWED_HOSTS=("*",)。 - outis nihil
哦,我的错。我没有注意到那个。谢谢你的纠正。 - Halil
这解决了我的问题。我一直收到400错误请求。谢谢@Andrey Zarubin。 - Bastin Robin
还要检查尾随空格,因为空格可能会导致错误。 - theTypan

7

由于我遇到了相同的问题(尝试使用vagrant share时出现400错误代码),我偶然发现了这个问题。答案和评论是正确的,明显的解决方案是设置 ALLOWED_HOSTS 列表,但我已经正确设置它了(我认为)。

我无法确定nginx因为我在apache2上运行,但以下解决方法解决了这个问题:

  1. Take a look at the ALLOWED_HOSTS doc to find what's best for your case.

  2. With vagrant, you might find it useful to accept all the vagrantshare.com subdomain, so just add '.vagrantshare.com' (notice the dot) to the ALLOWED_HOSTS list.

  3. Not sure if it is really necessary, but I changed the modified date of the wsgi.py file

    touch wsgi.py
    
  4. As I'm using apache2, I needed to restart the service.

    sudo service apache2 restart
    

然后它就正常工作了。


1
我遇到了这个问题。原因是我忘记在nginx配置中添加proxy_set_header设置:
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

所以Django没有看到原始请求的主机名,因此它与ALLOWED_HOSTS中的内容不匹配。然后它返回了400响应。

在我的nginx配置中添加这个(在你做proxy_pass到Gunicorn的地方),然后重新启动nginx,它就可以工作了。

更多信息:https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接