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

Nginx how to turn on Gzip Compression to greatly improve the Page loading Speed

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Editor to share with you how to turn on Nginx Gzip compression to greatly improve the page loading speed, I hope you will gain something after reading this article, let's discuss it together!

Environment: Debian 6

1. Vim, open the Nginx configuration file

Vim / usr/local/nginx/conf/nginx.conf

2. Find the following paragraph and modify it

Gzip on;gzip_min_length 1k MSIE buffers 4 16k switch gzipsets httppets version 1.0 cross gzipsets compounding level 2 partner gzipsets types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;gzip_vary off;gzip_disable "MSIE [1-6].

3. Explain.

Line 1: turn on Gzip

Line 2: do not compress the critical value. Only those larger than 1K are compressed. Generally, there is no need to change it.

Line 3: buffer, just, um, forget it, don't explain it, don't change it.

Line 4: if a reverse proxy is used, the end communication is HTTP/1.0, and those who need it should not read my popular science text; if you have this sentence, you can comment on it. The default is HTTP/1.1.

Line 5: compression level, 1-10. The larger the number, the better the compression, and the longer the compression time. Feel free to change your mood.

Line 6: the type of file for compression, just make up for what is missing. There are two ways to write JavaScript. It is best to write both. There are always people who complain that js files are not compressed. In fact, just write one more format.

Line 7: related to caching services such as Squid, on will add "Vary: Accept-Encoding" to Header. I don't need this. Let's see what happens.

Line 8: IE6 is not very friendly to Gzip and won't give it Gzip.

4. Save and exit wq and reload Nginx

/ usr/local/nginx/sbin/nginx-s reload

5. Use curl to test whether Gzip is enabled successfully.

Curl-I-H "Accept-Encoding: gzip, deflate"http://www.slyar.com/blog/"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:13:09 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.2.17p1X-Pingback: http://www.slyar.com/blog/xmlrpc.phpContent-Encoding: gzip

The page was compressed successfully

Curl-I-H "Accept-Encoding: gzip, deflate"http://www.slyar.com/blog/wp-content/plugins/photonic/include/css/photonic.css"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:25 GMTContent-Type: text/cssLast-Modified: Sun, 26 Aug 2012 15:17:07 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:25 GMTCache-Control: max-age=43200Content-Encoding: gzip

The css file was compressed successfully

Curl-I-H "Accept-Encoding: gzip, deflate"http://www.slyar.com/blog/wp-includes/js/jquery/jquery.js"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:21:38 GMTContent-Type: application/x-javascriptLast-Modified: Thu, 12 Jul 2012 17:42:45 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:21:38 GMTCache-Control: max-age=43200Content-Encoding: gzip

The js file was compressed successfully

Curl-I-H "Accept-Encoding: gzip, deflate"https://cache.yisu.com/upload/information/20200622/115/63231.png"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:22:45 GMTContent-Type: image/pngLast-Modified: Thu, 23 Aug 2012 13:50:53 GMTConnection: keep-aliveExpires: Tue, 25 Sep 2012 18:22:45 GMTCache-Control: max-age=2592000Content-Encoding: gzip

The picture was compressed successfully

Curl-I-H "Accept-Encoding: gzip, deflate"http://www.slyar.com/blog/wp-content/plugins/wp-multicollinks/wp-multicollinks.css"HTTP/1.1 200 OKServer: nginx/1.0.15Date: Sun, 26 Aug 2012 18:23:27 GMTContent-Type: text/cssContent-Length: 180Last-Modified: Sat, 02 May 2009 08:46:15 GMTConnection: keep-aliveExpires: Mon, 27 Aug 2012 06:23:27 GMTCache-Control: max-age=43200Accept-Ranges: bytes

Finally, there is a file of less than 1K. Because my threshold is 1K, it is not compressed.

Gzip parameter interpretation

Gzip on | off

Default value: gzip off

Turn the gzip module on or off

Gzip_static on | off

The processing module of static files in nginx

The module can read pre-compressed gz files, which can reduce the CPU resource consumption of gzip compression per request. After the module is enabled, nginx first checks whether there is a file at the end of the gz that requests the static file, and if so, returns the contents of the gz file directly. In order to be compatible with browsers that do not support gzip, enabling the gzip_static module must retain both the original static file and the gz file. In this way, when there are a large number of static files, the disk space will be greatly increased. We can make use of the reverse proxy function of nginx to keep only the gz file.

You can learn more about google "nginx gzip_static".

Gzip_comp_level 4

Default value: 1 (recommended is 4)

Gzip compression ratio / compression level, compression level 1-9. The higher the compression level, the greater the compression ratio, and of course, the longer the compression time (faster transmission but more cpu consumption).

Gzip_buffers 4 16k

Default value: gzip_buffers 4 4k/8k

Set up the system to get several units of cache to store the compressed result data stream of gzip. For example, 44k represents four times the amount of memory requested in 4k units according to the original data size. 48k represents 4 times the memory requested in 8k units according to the original data size in 8k units.

If it is not set, the default value is to request the same amount of memory space as the original data to store the gzip compression results.

Gzip_types mime-type [mime-type...]

Default: gzip_types text/html (js/css files are not compressed by default)

Compression type, matching MIME type for compression

Cannot use the wildcard character text/* (whether specified or not) text/html is compressed by default

Set which compressed text file can refer to conf/mime.types

Gzip_min_length 1k

Default value: 0, no matter how much the page is compressed

Sets the minimum number of bytes of a page that is allowed to be compressed, which is obtained from the Content-Length in the header header.

It is recommended to set the number of bytes greater than 1k. Less than 1k may increase the pressure. Namely: gzip_min_length 1024

Gzip_http_version 1.0 | 1.1

Default: gzip_http_version 1.1 (that is, only requests for HTTP/1.1 protocol will be compressed by gzip)

Identify the protocol version of the http. Since some early browsers or http clients may not support gzip self-decompression, users will see garbled code, so it is necessary to make some judgments.

Note: 99.99% of browsers basically support gzip decompression, so you don't have to set this value and keep the system default.

Suppose we use the default value of 1.1. if we use proxy_pass for reverse proxy, then the communication between nginx and backend upstream server is based on HTTP/1.0 protocol. If we use nginx to do Cache Server through reverse proxy, and the front-end nginx does not enable gzip, and our backend nginx does not set gzip_http_version to 1.0, then the url of Cache will not be compressed by gzip.

Gzip_proxied [off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any]...

Default value: off

When Nginx is enabled as a reverse proxy, the result returned by the back-end server is enabled or disabled. The premise of the match is that the back-end server must return a header header containing "Via".

Off-turn off compression of all agent result data

Expired-enables compression if the header header contains "Expires" header information

No-cache-enables compression if the header header contains "Cache-Control:no-cache" header information

No-store-enables compression if the header header contains "Cache-Control:no-store" header information

Private-enables compression if the header header contains "Cache-Control:private" header information

No_last_modified-enables compression if the header header does not contain "Last-Modified" header information

No_etag-enables compression if the header header does not contain "ETag" header information

Auth-enables compression if the header header contains "Authorization" header information

Any-unconditionally enable compression

Gzip_vary on

It has something to do with the http header. Add a vary header for the proxy server. Some browsers support compression, and some browsers do not support compression, so avoid wasting unsupported compression, so judge whether compression is needed according to the HTTP header of the client.

Gzip_disable "MSIE [1-6]."

Disable gzip compression for IE6 again because of miserable IE6. Of course, IE6 is still widespread, so here you can also set it to "MSIE [1-5]."

Some versions of IE6 do not support gzip compression very well, which will cause fake death of the page. Today, the students of the product tested this problem.

Later, after debugging, it was found that the gzip of img caused the fake death of IE6, and it was normal after the gzip compression of img was removed.

In order to ensure that there are no problems with other IE6 versions, it is recommended to add the setting of gzip_disable

After reading this article, I believe you have a certain understanding of "Nginx how to turn on Gzip compression to greatly improve page loading speed". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

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: 0

*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

Servers

Wechat

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

12
Report