nginx, uwsgi, DJango, 502 when DEBUG=False, “upstream prematurely closed connection”

試著忘記壹切 提交于 2019-12-24 13:25:31

问题


I have a working nginx production server running a Django app, using uwsgi (set up with this tutorial).

nginx and uwsgi are communicating through a UNIX socket.

However, as soon as I turn DEBUG = False in my Django settings, I get a 502 error. The nginx error log tells me:

2015/09/08 10:37:51 [error] 940#0: *4 upstream prematurely closed connection while reading response header from upstream, client: myIP, server: mydomain.ca, request: "GET /quests/ HTTP/1.1", upstream: "uwsgi://unix:///tmp/hackerspace.sock:", host: "myDomain"

How can I prevent the socket connection from timing out, and why is DEBUG = False making this difference?

Thanks!


回答1:


I found the solution that works for me. I had to specify hosts for ALLOWED_HOSTS list in django's settings.py

ALLOWED_HOSTS = ['example.com', 'example.dev']




回答2:


The "ALLOWED_HOSTS" answer also solved my problem. One thing to elaborate on, since it was not immediately clear to me anyways, the values you put here are the potential domain names (IPs, etc) that your site will be accessed by. If your site is http://mysite.here/ then you NEED to put "mysite.here" in the ALLOWED_HOSTS list. Apparently, with Debug=True there is no HOST validation, once it is switched to False the system starts rejecting any request where the value of HOST: header does not appear in the list. For further reading: https://docs.djangoproject.com/en/1.10/ref/settings/



来源:https://stackoverflow.com/questions/32464235/nginx-uwsgi-django-502-when-debug-false-upstream-prematurely-closed-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!