我在nginx里面配置了chunked_transfer_encoding on;可是请求的时候还是返回Content-Length,想问下在 nginx如何配置,使chunked_transfer_encoding生效
这个值默认就是on啊: http://nginx.org/en/docs/http/ngx_http_core_module.html#chunked_transfer_encoding
最后就是,你没理解chunked_transfer_encoding的含义,这个选项是否开启不会影响content-length。
我这个服务器也是用nginx实现反向代理的。我用curl请求,打印请求头看看:
~$ curl -I http://172.16.250.11/front/monitor HTTP/1.1 200 OK Server: nginx Date: Mon, 01 Feb 2016 11:55:21 GMT Content-Type: text/html;charset=UTF-8 Content-Length: 14651 Connection: keep-alive Vary: Accept-Encoding Set-Cookie: JSESSIONID=6A766B1362D24A17C2EC86D213FD01EA; Path=/; HttpOnly Set-Cookie: token=1454327721220; Path=/ Content-Language: en-US
~$ curl -I -H 'Accept-Encoding:gzip' http://172.16.250.11/front/monitor HTTP/1.1 200 OK Server: nginx Date: Mon, 01 Feb 2016 11:55:55 GMT Content-Type: text/html;charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding Set-Cookie: JSESSIONID=220A7F1826A75727EC9F8E2A3D058797; Path=/; HttpOnly Set-Cookie: token=1454327755777; Path=/ Content-Language: en-US Content-Encoding: gzip
可以看到当服务器使用gzip压缩的文件时,由于事先不知道文件大小,是没有Content-Length头的。请求的都是同一个资源 ######我把nginx当做一个web服务器,使用curl直接请求,没有 chunked_transfer_encoding,只有Content-Length###### 这个就是我请求的内容
[root@ ~]# curl -I http://172.16.0.217/index.html HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 02 Feb 2016 02:18:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 01 Feb 2016 07:34:41 GMT
Connection: keep-alive
ETag: "56af0a91-264"
Accept-Ranges: bytes
######补充一下,不要去纠结 Transfer-Encoding: chuncked这个头。其实最佳的方式是Vary: Accept-Encoding######因为这个文件是固定大小的啊,服务器可以事先知道文件大小,自然能返回Content-Length。你试试让服务器返回gzip压缩的文件。curl -H 'Accept-Encoding:gzip'再请求看看###### 还是不行,就连Content-Encoding都没有显示
[root@ conf]# curl -I -H 'Accept-Encoding:gzip' http://172.16.0.217/hls/1-1.ts HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Tue, 02 Feb 2016 03:17:53 GMT
Content-Type: video/mp2t
Content-Length: 750120
Last-Modified: Mon, 01 Feb 2016 09:45:43 GMT
Connection: keep-alive
ETag: "56af2947-b7228"
Accept-Ranges: bytes
######回复 @Feng_Yu : 你可以看看各种服务器的配置,几乎都对jpg,png,mp4等这一类多媒体文件不启用gzip压缩——实在是没必要######这个是对的,多媒体文件本身就有无损压缩的算法在里面,自身的冗余度很小,所以再用无损压缩算法gzip简直是吃力不讨好。所以多媒体文件几乎都不需要使用gzip,自然没有gzip编码啊###### 是因为我刚刚在配置文件没有配置gzip_types这个参数;配置上了之后,我在请求
< Server: nginx/1.8.0 < Date: Tue, 02 Feb 2016 03:34:50 GMT < Content-Type: video/mp2t < Last-Modified: Mon, 01 Feb 2016 09:45:43 GMT < Connection: keep-alive < ETag: W/"56af2947-b7228" < Content-Encoding: gzip * no chunk, no close, no size. Assume close to signal end ######这个竟然是 no chunk, no close, no size. Assume close to signal end###### @vicky1992
能否贴一下你的配置?我这边用curl跑就没这个问题
─$ curl -svH 'Accept-Encoding:gzip' 172.16.250.11/upload/docs/mytest.pdf -o /dev/null * Hostname was NOT found in DNS cache * Trying 172.16.250.11... * Connected to 172.16.250.11 (172.16.250.11) port 80 (#0) > GET /upload/docs/mytest.pdf HTTP/1.1 > User-Agent: curl/7.35.0 > Host: 172.16.250.11 > Accept: */* > Accept-Encoding:gzip > < HTTP/1.1 200 OK * Server nginx is not blacklisted < Server: nginx < Date: Tue, 02 Feb 2016 03:49:28 GMT < Content-Type: application/pdf < Content-Length: 981296 < Last-Modified: Tue, 02 Feb 2016 03:46:57 GMT < Connection: keep-alive < Expires: Tue, 09 Feb 2016 03:49:28 GMT < Cache-Control: max-age=604800 < Accept-Ranges: bytes < { [data not shown] * Connection #0 to host 172.16.250.11 left intact
我的配置
gzip on; gzip_min_length 1100; gzip_buffers 4 8k; gzip_types application/vnd.apple.mpegurl video/mp2t; output_buffers 1 32k; postpone_output 1460; ###### @vicky1992
对pdf文件启用了一下gzip。重新请求一次,这次是有chunked头的
我的nginx版本是1.8.1,最新稳定版本,绝大多数配置都用的是默认。
─$ curl -svH 'Accept-Encoding:gzip' 172.16.250.11/upload/docs/mytest.pdf -o /dev/null * Hostname was NOT found in DNS cache * Trying 172.16.250.11... * Connected to 172.16.250.11 (172.16.250.11) port 80 (#0) > GET /upload/docs/mytest.pdf HTTP/1.1 > User-Agent: curl/7.35.0 > Host: 172.16.250.11 > Accept: / > Accept-Encoding:gzip > < HTTP/1.1 200 OK * Server nginx is not blacklisted < Server: nginx < Date: Tue, 02 Feb 2016 03:54:03 GMT < Content-Type: application/pdf < Last-Modified: Tue, 02 Feb 2016 03:46:57 GMT < Transfer-Encoding: chunked < Connection: keep-alive < Vary: Accept-Encoding < Expires: Tue, 09 Feb 2016 03:54:03 GMT < Cache-Control: max-age=604800 < Content-Encoding: gzip < { [data not shown] * Connection #0 to host 172.16.250.11 left intact
配置文件的参考:
nginx.conf:
user www-data; worker_processes auto; pid /run/nginx.pid;
events { worker_connections 768; multi_accept on; }
http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; etag off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_types text/* application/json application/javascript application/xml application/xml+rss
application/x-javascript application/xhtml+xml application/vnd.ms-excel application/pdf;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/default: server { listen 80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; etag off; client_max_body_size 10m; client_body_buffer_size 128k; port_in_redirect off; ... 省略非关键部分 ... location ^~ /upload { if ($invalid_referer) { return 403; } alias /mnt/aircare; expires 7d; location ~* \.(png|jpg|gif)$ { error_page 404 /assets/unknown.png; error_page 415 /empty; set $width -; set $height -; if ($arg_w) { set $width $arg_w; } if ($arg_h) { set $height $arg_h; } image_filter resize $width $height; image_filter_buffer 10M; } location ^~ /upload/images/security { return 403; } } }
######我现在加上 'Accept-Encoding:gzip' 有chunked了,可是再去请求被nginx GZIP后的内容,发现解析不了######有压缩价值的基本也就是文本文件了,压缩率很高。其余二进制文件未必,需要自己实验。多媒体文件由于自带无损压缩算法,几乎不用gzip压缩。######请求过的内容是gzip压缩过的啊,你解压不就行了?浏览器会自动帮你解压,终端可以用curl | gunzip 即可
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。