nginx of gzip compression increase site speed

table of Contents:


Why use gzip compression

Nginx open the gzip compression, the size of the static resources webpage js, css, etc. will be greatly reduced thus saving a lot of bandwidth and improve transmission efficiency, fast user experience.

nginx achieve gzip

nginx resources to achieve compression works by default integrated ngx_http_gzip_module module intercepts the request, and needs to be done to make the type of gzip gzip, using very simple and straightforward to open, set the option. .

Gzip header after validation request and response headers

Request Headers:
Accept-Encoding:gzip,deflate,sdch

Response Headers:
Content-Encoding:gzip
Cache-Control:max-age240

gzip treatment process

From the perspective of the http protocol point of view, the request header statement acceopt-encoding: gzip deflate sdch (refers to the compression algorithm, which sdch google is a compression-way push of their own home)
server -> response -> the content compressed with gzip -> Send to the browser - "browser decoding gzip-> gzip compressed content is received

gzip common configuration parameters

  • gzip on | off is turned gzip
  • gzip_buffers 4k buffer (compression in the memory buffer a few? How each?)
  • gzip_comp_level [1-9] 6 recommended level of compression, the higher the level of compression is minimal, while the more waste cpu resources
  • gzip_disable regular match UA is what URi not gzip
  • gzip_min_length 200 begins to compress the minimum length is not less than the length of its compression nginx
  • gzip_http_version 1.0 | 1.1 starts to compress the http protocol version (default 1.1)
  • gzip_proxied set requestor proxy server, how cached content
  • gzip_types text / plain application / xml which types of files, such as compression txt, xml, html, css
  • gzip_vary off whether to transfer gzip compression flag

nginx configuration gzip

Static pages index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>演示nginx做gzip压缩</title>
    <script src="./jquery.js" ></script>
</head>
<body>
<img src="./nginx_img.jpeg" style="width: 100px;height: 100px;" />
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1>nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
</body>
</html>

nginx configuration

server{
        listen 80;
        server_name localhost 192.168.0.96;

        gzip on;
        gzip_buffers 32 4k;
        gzip_comp_level 6;
        gzip_min_length 200;
        gzip_types application/javascript application/x-javascript text/javascript text/xml text/css;
        gzip_vary off;

        root /Users/lidong/Desktop/wwwroot/test;

        index  index.php index.html index.htm;

        access_log /Users/lidong/wwwlogs/access.log;
        error_log /Users/lidong/wwwlogs/error.log;

        location ~ [^/]\.php(/|$) {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

}

For the front page using gzip request:

Gzip request to open the page:

note

  • Pictures, mp3 compression generally do not need, because the compression rate is relatively small
  • 一般压缩text,css,js,xml格式的文件
  • 比较小的文件不需要压缩,有可能还会比源文件更大
  • 二进制文件不需要压缩

Guess you like

Origin www.cnblogs.com/lisqiong/p/11387083.html