nginx 1.2.0启用gzip http压缩手记

本站的web在vps上的nginx里跑,之前为简单其见,没有配置http gzip压缩支持,但vps有时网络抽风而速度很慢,于是想起来配置gzip压缩支持。

网上有很多nginx启用gzip压缩的文章,但还是上官方看文档是最权威准确的。这两个页面:

http://wiki.nginx.org/HttpGzipModule
http://wiki.nginx.org/NginxHttpGzipStaticModule

nginx官方文档很易懂,先给一个典型的配置实例,下面再详讲每条指令,感觉相当亲切。

查看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。按我的配置,修改/usr/local/conf/nginx/nginx.conf,在http节点加入gzip配置即可。

    gzip_static on;
    gzip_http_version 1.1;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    gzip on;
    gzip_min_length 1000;
    gzip_types text/plain application/xml text/css text/javascript text/xml;

测试配置是否有语法错误

nginx -t

输出消息

nginx: the configuration file /usr/local/conf/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/conf/nginx/nginx.conf test is successful

确认无误,重新加载nginx配置

nginx -s reload

检测gzip压缩是否正常工作,使用live http headers ——firefox的一款插件,看http头是是否有这样一行:Content-Encoding: gzip

也可以使用firefox的firebug插件 的“网络”功能看,或者使用第三方的在线检测工具,如http://tool.chinaz.com/Gzips/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据