MySQL 错误:从上游读取响应头时上游过早关闭连接 [uWSGI/Django/NGINX]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22107726/
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
Error: upstream prematurely closed connection while reading response header from upstream [uWSGI/Django/NGINX]
提问by abisson
I am currently ALWAYS getting a 502 on a query my users are doing... which usually returns 872 rows and takes 2.07 to run in MySQL. It is however returning a LOT of information. (Each row contains a lot of stuff). Any ideas?
我目前总是在我的用户正在执行的查询中获得 502...通常返回 872 行并需要 2.07 才能在 MySQL 中运行。然而,它返回了大量信息。(每一行都包含很多东西)。有任何想法吗?
Running the Django (tastypie Rest API), Nginx and uWSGI stack.
运行 Django(tastypie Rest API)、Nginx 和 uWSGI 堆栈。
Server Config with NGINX
使用 NGINX 配置服务器
# the upstream component nginx needs to connect to
upstream django {
server unix:///srv/www/poka/app/poka/nginx/poka.sock; # for a file socket
}
# configuration of the server
server {
# the port your site will be served on
listen 443;
# the domain name it will serve for
server_name xxxx; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 750M; # adjust to taste
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /srv/www/poka/app/poka/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
UWSGI config
UWSGI 配置
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 2
# the socket (use the full path to be safe
socket = /srv/www/poka/app/poka/nginx/poka.sock
# ... with appropriate permissions - may be needed
chmod-socket = 666
# clear environment on exit
vacuum = true
pidfile = /tmp/project-master.pid # create a pidfile
harakiri = 120 # respawn processes taking more than 20 seconds
max-requests = 5000 # respawn processes after serving 5000 requests
daemonize = /var/log/uwsgi/poka.log # background the process & log
log-maxsize = 10000000
#http://uwsgi-docs.readthedocs.org/en/latest/Options.html#post-buffering
post-buffering=1
logto = /var/log/uwsgi/poka.log # background the process & log
回答by Danack
This is unlikely to be an nginx config issue.
这不太可能是 nginx 配置问题。
It's almost certainly that the backend is actually crashing (or just terminating the connection) rather than giving a malformed response. i.e. the error message is telling you what the problem is, but you're looking in the wrong place to solve it.
几乎可以肯定的是,后端实际上正在崩溃(或只是终止连接)而不是给出格式错误的响应。即错误消息告诉您问题是什么,但您正在寻找错误的地方来解决它。
You don't give enough information to allow use to figure out what the exact issue is but if I had to guess:
你没有提供足够的信息来让用户弄清楚确切的问题是什么,但如果我不得不猜测:
which usually returns 872 rows and takes 2.07 to run in MySQL. It is however returning a LOT of information.
它通常返回 872 行,在 MySQL 中运行需要 2.07。然而,它返回了大量信息。
It's either timing out somewhere or running out of memory.
它要么在某处超时,要么内存不足。
回答by emazzotta
I had the same issue, what fixed it for me is adding my domain in the settings.pye.g.:
我遇到了同样的问题,为我解决的是在settings.py 中添加我的域, 例如:
ALLOWED_HOSTS = ['.mydomain.com', '127.0.0.1', 'localhost']
By same issue, I mean I couldn't even load the page, nginx would return a 502 without serving any pages where I could cause the application to crash.
同样的问题,我的意思是我什至无法加载页面,nginx 会返回 502 而不提供任何可能导致应用程序崩溃的页面。
And the nginx log contained:
并且 nginx 日志包含:
Error: upstream prematurely closed connection while reading response header from upstream
回答by user553620
In your @django location block you can try adding some proxy read and connect timeout properties. e.g.
在您的@django 位置块中,您可以尝试添加一些代理读取和连接超时属性。例如
location @django {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
# proxy header definitions
...
proxy_pass http://django;
}
回答by u5755305
Sometimes it may be an authority problem. Check the project directory's authority.
有时可能是权限问题。检查项目目录的权限。
回答by Elia Ahadi
It might be an uwsgi configuration issue instead of Nginx. I saw that you had uwsgi processes = 2 and harakiri = 120, have you tried changing those as well as other fields there one by one?
这可能是 uwsgi 配置问题,而不是 Nginx。我看到您有 uwsgi processes = 2 和 harakiri = 120,您是否尝试过一一更改这些以及其他字段?
I had same issue but it wasn't my NGINX configuration, it was my UWSGI processes causing timeout errors when I posted JSONs from client side to server. I had processes as 5, I changed it to 1 and it solved the issue. For my application, I only needed to have 1 process run at time.
我遇到了同样的问题,但这不是我的 NGINX 配置,而是我的 UWSGI 进程在我将 JSON 从客户端发布到服务器时导致超时错误。我的流程为 5,我将其更改为 1 并解决了问题。对于我的应用程序,我只需要一次运行 1 个进程。
Here is the working UWSGI configuration autoboot ini file that solved the timeout issue and thus the 502 gateway issue (upstream closed prematurely).
这是工作的 UWSGI 配置自动引导 ini 文件,它解决了超时问题,从而解决了 502 网关问题(上游提前关闭)。
autoboot.ini
自动启动文件
#!/bin/bash
[uwsgi]
socket = /tmp/app.sock
master = true
chmod-socket = 660
module = app.wsgi
chdir = home/app
close-on-exec = true # Allow linux shell via uWSGI
processes = 1
threads = 2
vacuum = true
die-on-term = true
Hope it helps.
希望能帮助到你。