About Nginx gzip configuration

About Nginx gzip configuration

The principle of nginx to achieve resource compression is to intercept requests through the ngx_http_gzip_module module and perform gzip compression on the types that need gzip. This module is the default basic and does not need to be recompiled. You can directly enable it.

Basic Configuration

# Enable gzipgzip on;
 
# Enable the minimum file size for gzip compression. Files smaller than the set value will not be compressed. gzip_min_length 1k;
 
# gzip compression level, 1-9, the larger the number, the better the compression, but also the more CPU time it takes, detailed description will be given later gzip_comp_level 1;
 
# The file types to compress. JavaScript comes in many forms. The values ​​can be found in the mime.types file. gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
 
# Whether to add Vary: Accept-Encoding in the http header. It is recommended to turn on gzip_vary on;
 
# Disable IE 6 gzipgzip_disable "MSIE [1-6]\\.";
 
# Set the buffer size required for compression gzip_buffers 32 4k;
 
# Set the HTTP protocol version for gzip compression. It can be ignored if there is no load. # gzip_http_version 1.0;# Enable cache location ~* ^.+\\.(ico|gif|jpg|jpeg|png)$ {
  access_log off;
  expires 2d;
}
 
location ~* ^.+\\.(css|js|txt|xml|swf|wav)$ {
  access_log off;
  expires 24h;
}
 
location ~* ^.+\\.(html|htm)$ {
  expires 1h;
}
 
location ~* ^.+\\.(eot|ttf|otf|woff|svg)$ {
  access_log off;
  expires max;
}
 
# Format # expires 30s; # expires 30m; # expires 2h; # expires 30d;

Detection effect

1. Content-Encoding in nginx's response headers is gzip

2. The returned file size is obviously compressed

Compression instructions gzip on

Turn gzip on or off by default off Close code block http, server, location, if in location

gzip_buffers

Sets the number and size of buffers used to handle request compression. For example, 32 4K means applying for 32 times the memory space based on the size of one memory page in units of 4K (that is, the memory page in a system is 4K). It is recommended not to set this option and use the default value.

Syntax: gzip_buffers number size;
Default:
gzip_buffers 32 4k|16 8k;
Context: http, server, location

gzip_comp_level

Set the gzip compression level. The lower the level, the faster the compression speed and the smaller the file compression ratio. On the contrary, the slower the speed, the larger the file compression ratio.

Syntax: gzip_comp_level level;Default:
gzip_comp_level 1;Context: http, server, location

It is not the case that the higher the compression level, the better. In fact, the compression capability of gzip_comp_level 1 is sufficient. The higher the level, the smaller the increase in compression ratio. Instead, it will greatly reduce the processing performance.
On the other hand, compression must be combined with static resource caching to cache the compressed version. Otherwise, the server will not be able to handle the high load if compression is performed every time.

gzip_disable

Use an expression to indicate which UA headers should not be compressed with gzip.

Syntax: gzip_disable regex ...;
Default: —
Context: http, server,locationThis directive appeared in version 0.6.23.

gzip_min_length

Gzip compression will be used only when the returned content is larger than this value, in K. When the value is 0, all pages are compressed.

Syntax: gzip_min_length length;Default:
gzip_min_length 20;Context: http, server, location

gzip_http_version

Used to identify the version of the http protocol. Early browsers do not support gzip compression, and users will see garbled characters, so this option is added to support early versions. By default, gzip compression is not enabled under the http/1.0 protocol.

Syntax: gzip_http_version 1.0 | 1.1;
Default:
gzip_http_version 1.1;
Context: http, server, location

If there is another Nginx cluster as a load balancing layer in front of the application server, gzip is not enabled at this layer.
If we use proxy_pass for reverse proxy, the HTTP/1.0 protocol is used by default for communication between nginx and the backend upstreamserver.
If our Cache Server is also nginx, and the front-end nginx does not have gzip enabled.
At the same time, if gzip_http_version is not set to 1.0 on our backend nginx, the cached URL will not be gzip compressed.

gzip_proxied

When Nginx is used as a reverse proxy, enable:

1.off – turns off compression of all proxy result data 2. expired – enables compression if the header contains the "Expires" header 3.no-cache – enables compression if the header contains the "Cache-Control: no-cache" header 4.no-store – enables compression if the header contains the "Cache-Control: no-store" header 5. private – enables compression if the header contains the "Cache-Control: private" header 6. no_last_modified – enables compression if the header contains the "Last_Modified" header 7. no_etag – enables compression if the header contains the "ETag" header 8. auth – enables compression if the header contains the "Authorization" header 9.any – unconditionally compresses all result data
Syntax: gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
Default:
gzip_proxied off;
Context: http, server, location

gzip_types

Set the MIME type that needs to be compressed. Requests that are not within the set type range will not be compressed.

Syntax: gzip_types mime-type ...;
Default:
gzip_typestext/html;
Context: http, server, location

gzip_vary

Adding the response header "Vary: Accept-Encoding" tells the receiver that the data sent has been compressed. The effect of turning it on is to add Accept-Encoding:gzip to the response header, which is useful for client browsers that do not support gzip compression.

Syntax: gzip_varyon |off;
Default:
gzip_varyoff;
Context: http, server, location

This is the end of this article about how to enable gzip in Nginx. For more information about how to enable gzip in Nginx, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Nginx Gzip module enablement and configuration instructions
  • How to enable GZIP compression function on the server side (Apache, IIS, Nginx)
  • Complete steps to enable gzip compression in nginx
  • Nginx uses the Gzip algorithm to compress messages
  • Summary of using Nginx gzip command

<<:  The DOCTYPE mode selection mechanism of well-known browsers

>>:  Example code for implementing hollowing effect with CSS

Recommend

How to build nfs service in ubuntu16.04

Introduction to NFS NFS (Network File System) is ...

Detailed explanation of Vue custom instructions

Table of contents Vue custom directive Custom dir...

How to use CSS to center a box horizontally and vertically (8 methods)

Original code: center.html : <!DOCTYPE html>...

JavaScript Sandbox Exploration

Table of contents 1. Scenario 2. Basic functions ...

Introduction to the use of http-equiv attribute in meta tag

meta is an auxiliary tag in the head area of ​​htm...

Detailed explanation of concat related functions in MySQL

1. concat() function Function: Concatenate multip...

How to compare two database table structures in mysql

During the development and debugging process, it ...

The difference between MySQL database host 127.0.0.1 and localhost

Many of my friends may encounter a problem and do...

Summary of several situations in which MySQL indexes fail

1. Indexes do not store null values More precisel...

Linux uses binary mode to install mysql

This article shares the specific steps of install...

Steps for Vue3 to use mitt for component communication

Table of contents 1. Installation 2. Import into ...

Detailed explanation of the basic implementation principle of MySQL DISTINCT

Preface DISTINCT is actually very similar to the ...

Summary of Linux commands commonly used in work

Use more open source tools such as docker and kub...