Nginx(Gzip加速访问速度)

1. 编辑nginx配置文件:vi /usr/local/nginx/conf/nginx.conf, 找到如下内容:

  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]\.";

2.上面的内容解释:

  第一行:开启gzip 加速;

  第二行:压缩的临界值,默认是1k;

  第三行:gzip_buffers 的gzip缓存,默认即可;

  第四行:如果是反代理的话,末端通信是HTTP/1.0;

  第五行:压缩级别,1-10,数字越大越好,压缩的时间越长;

  第六行:要进行压缩的文件类型,需要什么就补上去即可;

  第七行:跟Squid等缓存服务有关,on的话,会在Header里增加了“Vary: Accept-Encoding”;

  第八行:不支持IE1-IE6;

3.保存 :wq , /usr/local/nginx/sbin/nginx -s reload (软重启,读取配置)

4.测试gzip是否配置成功:

  a) curl -I -H "Accept-Encoding: gzip, deflate" "http://127.0.0.1"(回车即可,这里是测试网站)

  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: http://127.0.0.1/index.php
  Content-Encoding: gzip

  页面成功压缩

  b) 图片的测试是否压缩成功:

   curl -I -H "Accept-Encoding: gzip, deflate" "http://127.0.0.1/uploads/Jackey.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

  图片成功压缩

#nginx 的 gzip 是用来加速网站访问速率的,因为会对gzip_type 指定的文件进行了压缩。

猜你喜欢

转载自www.cnblogs.com/Jackey-fighting/p/9139586.html