我使用如下视图通过 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
总之,客户端在 10 秒内得到响应,在大约 2 秒内下载所有数据,然后再挂起 18 秒。
并非巧合,我的 nginx 配置指定keepalive_timeout 20s;
. 等待keepalive_timeout秒后,内容完全OK。我可以通过将 keepalive_timeout 设置为零来“解决”这个问题,但这并不是一个真正可行的解决方案。
当内容很小(小于~1MB)时,问题就莫名其妙地消失了。
> GET "http://localhost/demo/awc.pdf?submissionType=addition&permit=5678" 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:04 --:--:-- 0< HTTP/1.1 200 OK
< Server: nginx/1.1.19
< Date: Wed, 30 Oct 2013 22:39:12 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 906k 0 906k 0 0 190k 0 --:--:-- 0:00:04 --:--:-- 246k* Connection #0 to host localhost left intact
我猜这与分块编码或缺少内容长度标头有关,但我似乎找不到魔法咒语。有任何想法吗?