nginx开启gzip网页资源压缩

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32447301/article/details/84170828

一、nginx开启日志功能
首先修改修改生成日志的格式,在nginx配置文件的http里添加如下内容:

log_format  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent $request_body "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"'
                     '$upstream_addr $upstream_response_time $request_time ';

然后修改nginx配置文件相应server配置

access_log  /logs/access.log   ;

二、nginx开启gizp功能

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 text/css application/xml text/javascript 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了
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

页面成功压缩

《参考:http://nginx.org/en/docs/http/ngx_http_gzip_module.html》
《参考:https://www.cnblogs.com/mitang/p/4477220.html》

猜你喜欢

转载自blog.csdn.net/qq_32447301/article/details/84170828