Nginx不输出正确的分块响应

我有一个expressjs在一些端点上做出部分回应的后端。

伪代码是这样的:

function (req, res, next) { res.writeHead(200, { 'Content-Type': 'application/json' }); request.a.file(function response (middleChunk) { res.write(middleChunk); }, function final (endingChunk) { res.end(endingChunk); }); } 

直接使用curl -v到expressjs实例就像一个魅力一样,逐步显示消息,最后endingChunk ;

但是我没有直接在我的主机上使用expressjs,但是我使用了一个使用nginx制作的反向代理,它的configuration就是这样的:

 server { listen 80; server_name funnyhost; root /directory; location ~/api/.* { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_read_timeout 600s; proxy_buffering off; } } 

proxy_buffering off使服务器响应一些事情:

 ~$ curl -v "domain.com/api/endpoint?testme=true" * About to connect() to domain.com port 80 (#0) * Trying XX.XX.XX.XX... connected > GET /api/endpoint?testme=true HTTP/1.1 > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 > Host: domain.com > Accept: */* > < HTTP/1.1 200 OK < Content-Type: application/json < Transfer-Encoding: chunked < Connection: keep-alive < Date: Mon, 12 Aug 2013 17:43:45 GMT < Server: nginx/1.5.3 < X-Powered-By: Express < 

但是Nginx正在等待FULL进展,直到expressjs代码在发送数据到客户端之前到达res.end()

我达到绝望的水平,我浪费了这个工作的时间:(希望有人可以帮助

其他信息

我在用着:

 ~# nginx -v nginx version: nginx/1.5.3 

Ubuntu的12.04 LTS服务器,node.js v0.10.15,expression3.3.5

按照要求

 # nginx -V nginx version: nginx/1.5.3 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-http_spdy_module --with-ipv6 --with-mail --with-mail_ssl_module --with-openssl=/build/buildd/nginx-1.5.3/debian/openssl-1.0.1e --add-module=/build/buildd/nginx-1.5.3/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.5.3/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.5.3/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.5.3/debian/modules/nginx-dav-ext-module --add-module=/build/buildd/nginx-1.5.3/debian/modules/nginx-cache-purge 

新闻

我试图遏制的可能性,所以我使用这个脚本https://gist.github.com/mrgamer/6222708

和下面的nginxconfiguration文件https://gist.github.com/mrgamer/6222734

在本地主机发出请求,一切正常,使得对远程VPS的请求响应头有不同的顺序和不同的行为; 响应打印出来全部。

我的本地电脑和远程VPS有两个Ubuntu的12.04与克里斯·Nginx PPA(launchpad.net /~chris-lea/+archive/nginx-devel),为了testing的目的,我在两个执行:

 ~# sudo aptitude purge nginx nginx-common && sudo aptitude install nginx -y 

下面列出“奇怪”的行为

本地主机testing标题是在正确的顺序和响应是正确的进步

 ~$ curl -v localhost.test * About to connect() to localhost.test port 80 (#0) * Trying 127.0.0.1... connected > GET / HTTP/1.1 > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 > Host: localhost.test > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.5.3 < Date: Tue, 13 Aug 2013 16:04:19 GMT < Transfer-Encoding: chunked < Connection: keep-alive < <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Chunked transfer encoding test</title></head><body><h1>Chunked transfer encoding test</h1><h5>This is a chunked response after 2 seconds. Should be displayed before 5-second chunk arrives.</h5> * Connection #0 to host localhost.test left intact * Closing connection #0 <h5>This is a chunked response after 5 seconds. The server should not close the stream before all chunks are sent to a client.</h5></body></html> 

远程testing

 ~$ curl -v domain.com * About to connect() to domain.com port 80 (#0) * Trying XX.YY.ZZ.HH... connected > GET / HTTP/1.1 > User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 > Host: domain.com > Accept: */* > < HTTP/1.1 200 OK < Transfer-Encoding: chunked < Connection: keep-alive < Date: Tue, 13 Aug 2013 16:06:22 GMT < Server: nginx/1.5.3 < <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>Chunked transfer encoding test</title></head><body><h1>Chunked transfer encoding test</h1><h5>This is a chunked response after 2 seconds. Should be displayed before 5-second chunk arrives.</h5> * Connection #0 to host test.col3.me left intact * Closing connection #0 <h5>This is a chunked response after 5 seconds. The server should not close the stream before all chunks are sent to a client.</h5></body></html> 

我很抱歉告诉你,这是一个个人连接问题:)

我使用的是3Gnetworking,而我的提供商(Italian TIM,bleh!)使用一些透明的代理来重新排列标题和caching响应。

他们也阻止了80端口的websockets,所以不能给客户提供有用的体验。

对不起serverfault!