Python 大型Nginx/uwsgi服务的内容挂起达keepalive\u超时秒

Python 大型Nginx/uwsgi服务的内容挂起达keepalive\u超时秒,python,django,nginx,uwsgi,Python,Django,Nginx,Uwsgi,我正在通过django 1.5.1使用以下视图提供动态生成的pdf: pdf = generate_pdf() response = HttpResponse(pdf, mimetype="application/pdf") response['Content-Disposition'] = 'attachment; filename=1234_2013_10_30.pdf' return response 100%的时间在开发服务器上工作。但是,我正在使用uwsgi版本1.9.18.2和ng

我正在通过django 1.5.1使用以下视图提供动态生成的pdf:

pdf = generate_pdf()
response = HttpResponse(pdf, mimetype="application/pdf")
response['Content-Disposition'] = 'attachment; filename=1234_2013_10_30.pdf'
return response
100%的时间在开发服务器上工作。但是,我正在使用uwsgi版本1.9.18.2和nginx版本1.1.19,并获得以下行为:

$ curl -v -o test.out "http://localhost/demo/awc.pdf?submissionType=addition&permit=1234"
...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /demo/awc.pdf?submissionType=addition&permit=1234 HTTP/1.1
> User-Agent: curl/7.21.2 (Windows) libcurl/7.21.2 OpenSSL/1.0.0a zlib/1.2.3
> Host: localhost
> Accept: */*
>
  0     0    0     0    0     0      0      0 --:--:--  0:00:10 --:--:--     0

< HTTP/1.1 200 OK
< Server: nginx/1.1.19
< Date: Wed, 30 Oct 2013 22:14:23 GMT
< Content-Type: application/pdf
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Language, Cookie
< Content-Language: en-us
< Content-Disposition: attachment; filename=1234_2013_10_30.pdf
<
{ [data not shown]
100 2313k    0 2313k    0     0  270k       0 --:--:--  0:00:12 --:--:--     0
.....
100 2313k    0 2313k    0     0  77452      0 --:--:--  0:00:30 --:--:--     0* transfer closed with outstanding read data remaining
100 2313k    0 2313k    0     0  75753      0 --:--:--  0:00:31 --:--:--     0* Closing connection #0

curl: (18) transfer closed with outstanding read data remaining

我猜这与分块编码或缺少内容长度标题有关,但我似乎找不到神奇的咒语。有什么想法吗?

仍然不确定为什么会出现原始问题,但找到了一个合适的解决方法:

在nginx配置中禁用分块传输编码似乎可以避免这个问题

location / {
    uwsgi_pass unix:///var/run/uwsgi/app/socket;
    include uwsgi_params;
    # keepalive_timeout 0;
    chunked_transfer_encoding off;
}

我也有同样的问题,但它也只发生在动态生成的内容上。我在nginx 1.5.8和Ubuntu12.04LTS上运行到php fpm的fastcgi_传递-我的keepalive_超时也导致了挂起

我认为keepalive_超时只(应该?)用于静态内容,但我正在欺骗我的动态javascript文件(js.php),好像它是静态的,所以keepalive超时被应用了

如果我能找到一个永久的解决方案,而不是这个解释…我会发布它。是答案,其中表示在nginx.conf文件中指定-

fastcgi_keep_conn on;
我仍然将我的keepalive_超时时间保留为仅1秒,但在接受的答案中提供的解决方案是有意义的——我的动态文件被传递给了fastcgi,但fastcgi没有保留keepalive连接

默认情况下,FastCGI服务器将在发送响应后立即关闭连接。但是,当此指令设置为on值时,nginx将指示FastCGI服务器保持连接打开。这对于保持与FastCGI服务器的连接正常运行尤其必要


经过进一步测试,这也不是一个可靠的解决方案。现在,我只想设置一个较低的keepalive_超时来避免这个问题。
fastcgi_keep_conn on;