您的位置:首页 > 运维架构 > Nginx

在windows、linux中开启nginx的Gzip压缩大大提高页面、图片加载速度

2017-10-18 17:32 736 查看
为了降低tomcat服务的压力,把页面上的图片采用windows版的nginx进行加载,由于有些图片比较大,加载特别的慢,所以在nginx中打开了gzip的压缩功能。加载图片的速度快了很多。

通过站长工具中的”网页GZIP压缩检测”工具检测图片的压缩率达到了69.53%,如下图:



下面介绍nginx.conf文件是怎么配置的:

1、打开nginx.conf配置文件;



2、找到#gzip on这句,如下图:



3.在把#gzip on 改成下面代码:

[java]
view plain
copy

print
?

#开启Gzip

gzip on;

#不压缩临界值,大于1K的才压缩,一般不用改

gzip_min_length 100k;

#设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间

gzip_buffers 4 48k;

#用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1

gzip_http_version 1.0;

#压缩级别,1-9,数字越大压缩的越好,时间也越长,看心情随便改吧

gzip_comp_level 6;

#设置需要压缩的MIME类型,非设置值不进行压缩

gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

#跟Squid等缓存服务有关,on的话会在Header里增加”Vary: Accept-Encoding”

gzip_vary off;

#IE6对Gzip不怎么友好,不给它Gzip了

gzip_disable ”MSIE [1-6]\.”;

#开启Gzip
gzip on;
#不压缩临界值,大于1K的才压缩,一般不用改
gzip_min_length 100k;
#设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间
gzip_buffers 4 48k;
#用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1
gzip_http_version 1.0;
#压缩级别,1-9,数字越大压缩的越好,时间也越长,看心情随便改吧
gzip_comp_level 6;
#设置需要压缩的MIME类型,非设置值不进行压缩
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#跟Squid等缓存服务有关,on的话会在Header里增加"Vary: Accept-Encoding"
gzip_vary off;
#IE6对Gzip不怎么友好,不给它Gzip了
gzip_disable "MSIE [1-6]\.";


改成后代码如下图:



4.重新加载Nginx;

[java]
view plain
copy

print
?

<span style=“white-space:pre”> </span>在linux中重启:/usr/local/nginx/sbin/nginx -s reload

在windows中重启:C:\server\nginx-1.0.2>nginx.exe -s reload

<span style="white-space:pre">       </span>在linux中重启:/usr/local/nginx/sbin/nginx -s reload
在windows中重启:C:\server\nginx-1.0.2>nginx.exe -s reload


5.测试nginx压缩是否启用:

(1).页面成功压缩

[java]
view plain
copy

print
?

<span style=“white-space:pre”> </span>curl -I -H “Accept-Encoding: gzip, deflate” “http://wwww.xxxx.com/mr_smile2014”

HTTP/1.1 200 OK

Server: nginx/1.9.1

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

<span style="white-space:pre">  </span>curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014"
HTTP/1.1 200 OK
Server: nginx/1.9.1
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
(2).css文件成功压缩

[java]
view plain
copy

print
?

curl -I -H “Accept-Encoding: gzip, deflate” “http://wwww.xxxx.com/mr_smile2014/index.css”

HTTP/1.1 200 OK

Server: nginx/1.9.1

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

curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014/index.css"

HTTP/1.1 200 OK
Server: nginx/1.9.1
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


(3).图片成功压缩

[java]
view plain
copy

print
?

curl -I -H “Accept-Encoding: gzip, deflate” “http://wwww.xxxx.com/1_mr_smile2014.jpg”

HTTP/1.1 200 OK

Server: nginx/1.9.1

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" "http://wwww.xxxx.com/1_mr_smile2014.jpg"

HTTP/1.1 200 OK
Server: nginx/1.9.1
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


(4)js文件成功压缩

[java]
view plain
copy

print
?

curl -I -H “Accept-Encoding: gzip, deflate” “http://wwww.xxxx.com/mr_smile2014/js/jquery/jquery.js”

HTTP/1.1 200 OK

Server: nginx/1.9.1

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

curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014/js/jquery/jquery.js"

HTTP/1.1 200 OK
Server: nginx/1.9.1
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: