在我的nginx.conf中有:
gzip on; gzip_static on; gzip_buffers 16 8k; gzip_comp_level 9; gzip_http_version 1.0; gzip_min_length 1000; gzip_types text/plain text/css image/x-icon image/bmp image/png image/gif image/jpeg image/jpg application/json application/x-javascript text/javascript; gzip_vary on; gzip_proxied any;
所以,如果我在我的服务器上获取图片的标题:
spiroo@glamdring:~$ curl -I http://static.mysite.com/g/pics/big_6e1855d844ebca560379139e75942f669655f.jpeg HTTP/1.1 200 OK Server: nginx Date: Thu, 04 Apr 2013 13:00:20 GMT Content-Type: image/jpeg Content-Length: 5336 Last-Modified: Mon, 25 Mar 2013 13:28:02 GMT Expires: Fri, 04 Apr 2014 13:00:20 GMT Cache-Control: max-age=31536000 Pragma: public Cache-Control: public, must-revalidate, proxy-revalidate Accept-Ranges: bytes
但是,如果我closuresnginx.conf中的gzip压缩,则Content-Length上的结果完全相同。
我究竟做错了什么?
提前致谢。
PS:要清楚的是,我有nginx的最后一个版本,我在一个代理(haproxy)后面。
编辑==>
我有与CSS相同的问题。 我知道jpeg已经被压缩了。 当我切换gzip开关时,我重新启动了nginx。
这是我从一个CSS文件的标题获取。 我有相同的内容长度有或没有gzip压缩。
spiroo@glamdring:~$ curl -I http://static.mysite.com/css/9a7f503b.css HTTP/1.1 200 OK Server: nginx Date: Thu, 04 Apr 2013 14:21:50 GMT Content-Type: text/css Content-Length: 203088 Last-Modified: Tue, 02 Apr 2013 11:34:39 GMT Vary: Accept-Encoding Expires: Fri, 04 Apr 2014 14:21:50 GMT Cache-Control: max-age=31536000 Pragma: public Cache-Control: public, must-revalidate, proxy-revalidate Accept-Ranges: bytes
这是我的静态文件的nginxconfiguration:
server { server_name static.mysite.com; root /home/www/mysite/current/web; location / { return 404; } location ~ \.(?:jpg|jpeg|js|css|gif|png|swf|ico|pdf)$ { expires 365d; access_log off; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; }
}
curl不Accept-Encoding: gzip默认情况下。 你需要使用-H "Accept-Encoding: gzip,deflate"来获得curl请求的gzip或更好的,使用--compressed ,curl会知道解压结果。
图片(jpg / gif等)已经被压缩了。 所以你不需要(也不应该试图)在Web服务器上压缩它们。
这里是我压缩的一个例子:
gzip_types text/html text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp;