我们有一个Node / express Web应用程序,除了正常内容之外,还通过express.static()提供静态资产.前面有一个Nginx服务器,当前配置为gzip这些静态资产请求,如果用户代理为它.
但是,虽然Nginx正在按预期执行gzip,但是它会从原点删除Content-Length标头,并设置Transfer-Encoding:chunked.这打破了我们CDN的缓存.
以下是典型静态资产请求(本例中为JS文件),节点后端和Nginx的响应:
请求:
curl -s -D - 'http://my_node_app/res/my_js.js' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Connection: keep-alive' --compressed -o /dev/null
节点的响应标头:
HTTP/1.1 200 OK
Accept-Ranges: bytes
Date: Wed,07 Jan 2015 02:24:55 GMT
Cache-Control: public,max-age=0
Last-Modified: Wed,07 Jan 2015 01:12:05 GMT
Content-Type: application/javascript
Content-Length: 37386 // <--- The expected header
Connection: keep-alive
HTTP/1.1 200 OK
Server: Nginx
Date: Wed,07 Jan 2015 02:24:55 GMT
Content-Type: application/javascript
Transfer-Encoding: chunked // <--- The problematic header
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: public,07 Jan 2015 01:12:05 GMT
Content-Encoding: gzip
我们当前的静态资产位置的Nginx配置如下所示:
Nginx配置:
# cache file paths that start with /res/
location /res/ {
limit_except GET HEAD { }
# http://Nginx.com/resources/admin-guide/caching/
# http://Nginx.org/en/docs/http/ngx_http_proxy_module.html
proxy_buffers 8 128k;
#proxy_buffer_size 256k;
#proxy_busy_buffers_size 256k;
# The cache depends on proxy buffers,and will not work if proxy_buffering is set to off.
proxy_buffering on;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_connect_timeout 2s;
proxy_read_timeout 5s;
proxy_pass http://node_backend;
chunked_transfer_encoding off;
proxy_cache my_app;
proxy_cache_valid 15m;
proxy_cache_key $uri$is_args$args;
}
从上面的配置中可以看出,即使我们根据Nginx文档显式设置chunked_transfer_encoding为这些路径,启用了proxy_buffering,并且具有足够大的proxy_buffers大小,响应仍然被分块.
我们在这里失踪了什么?
– 编辑1:版本信息 –
$Nginx -v
Nginx version: Nginx/1.6.1
$node -v
v0.10.30
– 编辑2:Nginx gzip config–
# http://Nginx.org/en/docs/http/ngx_http_gzip_module.html
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 1;
gzip_min_length 1000;
#gzip_http_version 1.0;
gzip_types application/javascript text/css
gzip_proxied any;
gzip_vary on;