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

时间:2022-12-02 00:49:14

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

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

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

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

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

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

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

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

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

#开启Gzip
gzip on;
#不压缩临界值,大于10K的才压缩,一般不用改
gzip_min_length 10k;
#设置gzip申请内存的大小,其作用是按块大小的倍数申请内存空间
gzip_buffers 48k;
#用了反向代理的话,末端通信是HTTP/1.0,有需求的应该也不用看我这科普文了;有这句的话注释了就行了,默认是HTTP/1.1
gzip_http_version 1.0;
#压缩级别,-,数字越大压缩的越好,时间也越长,看心情随便改吧
gzip_comp_level ;
#设置需要压缩的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]\.";

改成后代码如下图:

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

4.重新加载Nginx;

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

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

(1).页面成功压缩

<span style="white-space:pre">    </span>curl -I -H "Accept-Encoding: gzip, deflate" "http://wwww.xxxx.com/mr_smile2014"
HTTP/1.1 OK
Server: nginx/1.9.
Date: Sun, Aug :: GMT
Content-Type: text/html; charset=UTF-
Connection: keep-alive
X-Powered-By: PHP/5.2.17p1
X-Pingback: http://www.slyar.com/blog/xmlrpc.php
Content-Encoding: gzip

(2).css文件成功压缩

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

HTTP/1.1  OK
Server: nginx/1.9.
Date: Sun, Aug :: GMT
Content-Type: text/css
Last-Modified: Sun, Aug :: GMT
Connection: keep-alive
Expires: Mon, Aug :: GMT
Cache-Control: max-age=
Content-Encoding: gzip

(3).图片成功压缩

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

HTTP/1.1  OK
Server: nginx/1.9.
Date: Sun, Aug :: GMT
Content-Type: image/png
Last-Modified: Thu, Aug :: GMT
Connection: keep-alive
Expires: Tue, Sep :: GMT
Cache-Control: max-age=
Content-Encoding: gzip

(4)js文件成功压缩

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

HTTP/1.1  OK
Server: nginx/1.9.
Date: Sun, Aug :: GMT
Content-Type: application/x-javascript
Last-Modified: Thu, Jul :: GMT
Connection: keep-alive
Expires: Mon, Aug :: GMT
Cache-Control: max-age=
Content-Encoding: gzip

原贴地址:https://blog.csdn.net/mr_smile2014/article/details/51983447