本文转自http://www.veryhuo.com/a/view/51706.html,如有侵权,请及时联系转载人删除!

在实际运维中,为了提高web页面的访问加载速度,一般会把静态资源(比如js、css、img等)在服务器进行压缩再传输到客户浏览器,主要配置步骤如下:

环境:Centos

1、Vim打开Nginx配置文件

vim /etc/nginx/nginx.conf

2、找到如下一段,进行修改

gzip on; // 开启Gzip
gzip_min_length 1k; // 不压缩临界值,大于1K的才压缩,一般不用改
gzip_buffers 4 16k; // buffer,就是,嗯,算了不解释了,不用改
#gzip_http_version 1.0; // 用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1
gzip_comp_level 2; // 压缩级别,1-10,数字越大压缩的越好,时间也越长!
gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png; // 进行压缩的文件类型,缺啥补啥就行了,JavaScript有两种写法,最好都写上吧,总有人抱怨js文件没有压缩,其实多写一种格式就行了
gzip_vary off; // 跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding",我不需要这玩意,自己对照情况看着办吧
gzip_disable "MSIE [1-6]\."; // IE6对Gzip不怎么友好,不给它Gzip了

3、:wq保存退出,重新加载Nginx

nginx -s reload

4、用curl测试Gzip是否成功开启

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/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: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip

页面成功压缩

curl -I -H "Accept-Encoding: gzip, deflate" "http://www.slyar.com/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" "http://www.slyar.com/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" "http://www.slyar.com/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

图片成功压缩

最新文章

  1. 使用Eclipse将Web项目打Jar包方法
  2. git 基于发布分支的开发
  3. CodeForces 164C Machine Programming 费用流
  4. 改动分区后,导致linux上不了
  5. ESPlatform 支持的三种群集模型 —— ESFramework通信框架 4.0 进阶(09)
  6. MySQL --当AUTO_INCREMENT自增遇到0
  7. windows下载安装MariaDB10.2.17 绿色版
  8. 虚拟机环境配置(Docker)
  9. 入门NodeJS
  10. Java 集合细节(二):asList 的缺陷
  11. L1-015 跟奥巴马一起画方块
  12. oletools下载安装及rtfobj使用
  13. 聊聊jvm系列
  14. Android中使用MediaCodec硬件解码,高效率得到YUV格式帧,快速保存JPEG图片(不使用OpenGL)(附Demo)
  15. 时间序列挖掘-预测算法-三次指数平滑法(Holt-Winters)——三次指数平滑算法可以很好的保存时间序列数据的趋势和季节性信息
  16. 关于$NOIP2017$的题目讲解
  17. CSS3:图片水平垂直居中
  18. vs2017通过模块文件添加自定义注释
  19. Java装饰者模式(Decorator)
  20. Fitnesse中TemplateLibrary的使用方法

热门文章

  1. mvc 自定义 AuthorizeAttribute 验证逻辑
  2. DAL 层引用 System.Net.Http ,引发的一阵心慌
  3. PLSQ创建用户L
  4. [翻译]Nativescript 中 Web 视图与 Android/IOS 的双向通信
  5. Post 提交用户名和密码, 用户自动登录,无需手动登录
  6. LibreOJ #2036. 「SHOI2015」自动刷题机
  7. 洛谷P4004 Hello world!(分块+并查集)
  8. PJzhang:kali linux安装网易云音乐、Visual Studio Code、skype
  9. 笔记-JavaWeb学习之旅16
  10. VRTK3.3.0-002获取手柄事件