查看ngix编译参考,是否启用了gzip模块,运行命令:
代码如下 | 复制代码 |
/path/to/binary/nginx -V |
(我的nginx在PATH目录里,故命令中可以省略nginx所在的目录),写为nginx -t 即可,下文从略写
注意其中是否 --with-http_gzip_static_module,如果没有,那就自己编译一次吧,下面是我的配置参数,供参考。最后的--add-module=../nginx-lib/nginx-upload-progress-module-v0.9.0/是nginx上传进度模块,nginx第三方模块,源码放置在../nginx-lib/nginx-upload-progress-module-v0.9.0目录,如果你没有下载过该模块,去掉它就可以了。
代码如下 | 复制代码 |
nginx version: nginx/1.2.0 configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/conf/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_stub_status_module --with-http_gzip_static_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_random_index_module --with-cpu-opt=pentium4 --add-module=../nginx-lib/nginx-upload-progress-module-v0.9.0/ |
修改nginx配置文件,nginx配置文件目录参看nginx配置参数里的--conf-path
nginx要修改两处,在虚拟主机配置段中添加:
代码如下 | 复制代码 |
location ~* ^.+.gzjs$ { add_header Content-Encoding gzip; gzip off; } |
另外再修改nginx的mime文件,在application/x-javascript js;中添加gzjs
代码如下 | 复制代码 |
application/x-javascript js gzjs; |
我的配置代码
代码如下 | 复制代码 |
gzip on; gzip_min_length 1k; gzip_buffers 8 32k; gzip_http_version 1.0; gzip_comp_level 5; gzip_types text/plain application/x-javascript text/javascript text/css application/xml; gzip_vary on; |
注解
1.开启Gzip
2.临界值小于1k 的文件不压缩,0为全压缩。
3.buffer大小
4.用于识别http协议的版本,早期的浏览器不支持gzip压缩,用户会看到乱码,所以为了支持前期版本加了此选项,目前此项基本可以忽略
5.压缩级别,1-9,数字越大压缩率越高越费CPU。
6.设置需要压缩的MIME类型,非设置值不进行压缩。JavaScript 两种写法都添加了,网络图片本身都是压缩格式了就不再压缩了。
7.加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持。因此,为避免浪费不支持的也压缩,需要根据客户端的HTTP头来判断,是否需要压缩。
测试
代码如下 | 复制代码 |
curl -I http://www.111cn.net/ -H Accept-Encoding:gzip,defalte |
下在补充
#nginx开启gzip压缩,放在location内
代码如下 | 复制代码 |
gzip on; gzip_min_length 1000; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/text text/html text/plain text/xml text/css application/x-javascript application/javascript; |
环境:Debian 6
1、Vim打开Nginx配置文件
代码如下 | 复制代码 |
vim /usr/local/nginx/conf/nginx.conf |
2、找到如下一段,进行修改
代码如下 | 复制代码 |
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; #gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary off; gzip_disable "MSIE [1-6]."; |
用curl测试Gzip是否成功开启
curl -I -H "Accept-Encoding: gzip, deflate" "/blog/"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:13:09 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: /blog/xmlrpc.php
Content-Encoding: gzip
页面成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "/blog/wp-content/plugins/photonic/include/css/photonic.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:25 GMT
Content-Type: text/css
Last-Modified: Sun, 26 Aug 2012 15:17:07 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:25 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
css文件成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "/blog/wp-includes/js/jquery/jquery.js"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:21:38 GMT
Content-Type: application/x-javascript
Last-Modified: Thu, 12 Jul 2012 17:42:45 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:21:38 GMT
Cache-Control: max-age=43200
Content-Encoding: gzip
js文件成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "/blog/wp-content/uploads/2012/08/2012-08-23_203542.png"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:22:45 GMT
Content-Type: image/png
Last-Modified: Thu, 23 Aug 2012 13:50:53 GMT
Connection: keep-alive
Expires: Tue, 25 Sep 2012 18:22:45 GMT
Cache-Control: max-age=2592000
Content-Encoding: gzip
图片成功压缩
curl -I -H "Accept-Encoding: gzip, deflate" "/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"
HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Sun, 26 Aug 2012 18:23:27 GMT
Content-Type: text/css
Content-Length: 180
Last-Modified: Sat, 02 May 2009 08:46:15 GMT
Connection: keep-alive
Expires: Mon, 27 Aug 2012 06:23:27 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes