Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Setting method of Gzip Compression for Nginx performance Optimization

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "Nginx performance optimization of Gzip compression settings", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the setting method of Gzip compression for Nginx performance optimization.

Nginx turns on the Gzip compression function, which can compress the css, js, xml and html files of the website during transfer, improve access speed, and then optimize Nginx performance! Web website pictures, videos and other multimedia files, as well as large files, because the compression effect is not good, so there is no need for image compression, if you want to optimize, you can set the life cycle of the image longer, let the client cache. When the Gzip feature is enabled, the Nginx server will compress the sent content, such as css, js, xml, html and other static resources according to the configured policy, so as to reduce the size of the content. Before the user receives the returned content, it will be processed to present the compressed data to the customer. This can not only save a lot of exit bandwidth and improve transmission efficiency, but also improve users' fast perceptual experience and kill two birds with one stone; although it will consume some cpu resources, it is worth it to give users a better experience.

After Gzip compression, the page size can be reduced to 30% or less, so that users can browse the page much faster. The compressed page of Gzip needs to be supported by both the browser and the server, which is actually compressed on the server side, decompressed and parsed by the browser after it is transmitted to the browser. We don't need to worry about browsers, because most browsers now support parsing Gzip pages.

Gzip compression: the compression function can be enabled before sending the response message to the client, which can effectively save bandwidth and improve the speed of the response client. Gzip compression can be configured under http,server and location modules. Nginx enables Gzip compression parameters description:

Gzip on; # decides whether to open the gzip module, on means on, off means off; gzip_min_length 1k; # sets the minimum bytes of pages allowed to be compressed (obtained from the Content-Length of the header header). When the returned content is greater than this value, gzip will be used for compression. K as the unit, when the value is 0, all pages are compressed. It is recommended to be greater than 1kgzip_buffers 4 16k; # set the gzip request memory size, which is used to apply for memory space according to multiples of the block size, followed by k after param2:int (k). Here, use 16k as the unit, apply for memory gzip_http_version 1.1 according to 4 times the original data size of 16k; # identify the version of http protocol, early browsers may not support gzip self-decompression, users will see garbled gzip_comp_level 2; # set gzip compression level, the lower the compression speed, the smaller the file compression ratio, and the slower the speed, the greater the file compression ratio Level 1-9, the smallest compression is the fastest but consumes cpugzip_types text/plain application/x-javascript text/css application/xml; # sets the MIME type that needs to be compressed, and the non-set value does not compress, that is, the matching compression type gzip_vary on; # enables the response header "Vary: Accept-Encoding" gzip_proxied off Enabled when nginx is a reverse proxy, off (turns off compression of all proxy results), expired (enables compression, if the header header includes "Expires" header information), no-cache (enables compression, header header contains "Cache-Control:no-cache"), no-store (enables compression, header header contains "Cache-Control:no-store"), private (enables compression, header header contains "Cache-Control:private") No_last_modefied (enable compression, header header does not contain "Last-Modified"), no_etag (enable compression, if header header does not contain "Etag" header information), auth (enable compression, if header header contains "Authorization" header information) gzip_disable msie6 (IE5.5 and IE6 SP1 use the msie6 parameter to disable gzip compression) specify which browsers do not need gzip compression (will match User-Agents) Depending on the PCRE library # is as follows: modify nginx configuration file / Usr/local/nginx/conf/nginx.conf [root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf # put the following configuration in the http {...} area of nginx.conf # modify the configuration to gzip on # enable gzip compression function gzip_min_length 10k; # set the minimum number of bytes of pages allowed to be compressed; here means that if the file is less than 10 bytes, there is no need to compress, because it is meaningless, it is already very small .gzip _ buffers 4 16k; # set the compression buffer size, here set to 4 16K memory as the compression result stream cache gzip_http_version 1.1 # compressed version gzip_comp_level 2; # set compression ratio to a minimum of 1, fast processing speed and slow transmission speed 9 is the maximum compression ratio, and the processing speed is slow and the transmission speed is fast. Here, the compression level can be any one of 0 to 9. The higher the compression level, the smaller the compression, saving bandwidth resources, but also consuming CPU resources, so the general compromise is 6gzip types text/css text/xml application/javascript. # define the type of compression, and configure as many compression types as possible in online configuration! gzip_disable "MSIE [1-6]\."; # configure disable gzip conditions and support regularities. This means that gzip is not enabled for ie6 and below (because the lower version of ie does not support it) gzip vary on; # Select the option to support vary header; modification to allow the front-end cache server to cache gzip-compressed pages; this can be left unwritten, indicating that I used gzip compression to the client when transferring data

The following are the Gzip compression configurations commonly used online

[root@external-lb02] # cat / data/nginx/conf/nginx.conf.http {. Gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json; gzip_disable "MSIE [1-6]\."; gzip_vary on;}

If you do not enable Gzip compression (that is, comment out the relevant configuration of Gzip), view a picture size

[root@external-lb02] # ll-h / data/web//www/test.bmp-rw-r--r-- 1 root root 453K March 14 18:43 / data/web//www/test.bmp

As shown below, the file is not compressed, and the file transfer size is still more than 400 K

If you turn on the Gzip compression function of Nginx (that is, open the relevant configuration of Gzip), and then visit the test.bmp picture again, it is found that the compressed image file transfer size is only 200K!

Through the comparison of the above tests, it is found that after Nginx turns on the Gzip compression function, the file size of the defined gzip type is obviously smaller when it is transferred, which will greatly improve the nginx access performance.

Test the command directly with curl:

[root@fvtlb02] # curl-I-H "Accept-Encoding: gzip, deflate"http://fvtvfc-web.kevin.com/service-worker.js"HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 26 Nov 2018 02:19:16 GMTContent-Type: application/javascript; charset=utf-8Connection: keep-aliveVary: Accept-EncodingLast-Modified: Sun, 25 Nov 2018 22:28:15 GMTVary: Accept-EncodingETag: W /" 5bfb21ff-40be "Content-Encoding: gzip

As above, "Conten_Encoding: gzip" appears in the response header header information, which means that Nginx has enabled compression (in browser access, the response header of the request is the same through F12)

Although Nginx's Gzip compression feature works well, it is not recommended to enable this compression feature for the following two types of file resources.

1) reason for image type resources (and video files): images such as jpg and png files will be compressed, so even if gzip is enabled, the size before compression is not much different from that after compression, so enabling it will be a waste of resources. Try compressing a jpg image to zip, and observe that the size doesn't change much. Although the zip and gzip algorithms are different, it can be seen that the value of compressed images is not great)

2) the reason for large file resources: it will consume a lot of cpu resources, and may not necessarily have obvious effect.

At this point, I believe that you have a deeper understanding of the setting method of Gzip compression for Nginx performance optimization, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 261

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report