Nginx uses the Gzip algorithm to compress messages

Nginx uses the Gzip algorithm to compress messages

What is HTTP Compression

Sometimes, relatively large message data is transmitted between the client and the server, which takes up a large amount of network bandwidth and time. In order to save bandwidth and speed up the response speed of the message, the message data to be transmitted can be compressed before transmission.

HTTP supports multiple message compression algorithms. The following is a common request header. From the Accept-Encoding field, we can see that it supports gzip, deflate, and br compression algorithms. In this article, we focus on using the Gzip algorithm to compress messages, such as Gzip to compress HTML, Javascript, and CSS files. After compression, it can greatly reduce the amount of data transmitted over the network and increase the speed at which users display web pages.

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost:8000
If-Modified-Since: Tue, 21 Apr 2020 14:09:01 GMT
If-None-Match: "5e9efe7d-264"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36

Any technology has two sides. Although HTTP compression can reduce bandwidth usage and speed up response speed, it will occupy some computing resources on the client or server because it requires additional compression and decompression processes.

Readers who are familiar with HTTP know that HTTP message bodies can be encoded and encrypted. In fact, HTTP compression is a special encoding method. Using this encoding method can greatly reduce the message size, and using the corresponding solution method can restore the original message. (We can see that the essence of compression technology is actually a kind of encoding method)

HTTP compression usage scenarios

From the above introduction to HTTP compression, we can see that this technology is an optimization technology, which is often used to compress the messages returned by the server to save bandwidth and speed up the response.

The following is a brief introduction to the process of using Gzip compression in HTTP.

  • The browser sends an HTTP request to the Web server, and the request contains Accept-Encoding: gzip, deflate, br. (tell the server that the browser supports gzip compression)
  • After receiving the request, the web server first generates the original Response, which contains the original Content-Type and Content-Length.
  • The web server encodes the Response through Gzip. After encoding, the header contains Content-Type and Content-Length (compressed size), and Content-Encoding: gzip is added. Then the Response is sent to the browser.
  • After receiving the Response, the browser decodes the Response according to Content-Encoding:gzip. After getting the original response, the web page is displayed.

The client can also send compressed data to the server, and decompress the request data through code. For standardization, Content-Encoding:gzip should also be added to the request.

Implementing HTTP Compression with Nginx

Nginx provides support for HTTP Gzip compression. Here we will see how to adapt Nginx to compress the return message.

Nginx supports the Gzip function through the ngx_http_gzip_module module, ngx_http_gzip_static_module module and ngx_http_gunzip_module module. Generally, Nginx will compile these modules by default. You can use the nginx -V command to see whether your installed nginx contains these modules.

Gzip related instructions can be in the http block, server block or location block of the configuration file.

ngx_http_gzip_module module

The ngx_http_gzip_module module is mainly responsible for enabling and setting the Gzip function and performing online real-time compression on the response data. This module contains the following main directives.

# Enable or disable the Gzip function. By default, this command is set to off, which means that the Gzip function is not enabled. Only when this directive is set to on, other directive settings are effective gzip on | off

# Set the size of the cache space used by Gzip compressed files. # The default value is: gzip_buffers 32 4k|16 8k
gzip_buffers number size;

# This command is used to set the Gzip compression level, including level 1 to level 9.
# Level 1 means the lowest compression level and the highest compression efficiency; level 9 means the highest compression level, the lowest compression efficiency and the longest time consumption.
# Default is 1
gzip_comp_level level

# For requests initiated by different types of clients, the Gzip function can be selectively enabled or disabled.
#Supports regular expressions, where regex is set based on the client's browser flag (User-Agent, UA).
gzip_disable regex ...;

# This setting uses a regular expression that matches all browsers that contain MSIE 4, MSIE 5, and MSIE6 in the UC string.
# When responding to requests from these browsers, the Nginx server does not perform Gzip compression.
gzip_disable MSIE [4-6]\.;

# Some early browsers or HTTP clients may not support Gzip self-decompression, so users sometimes see garbled characters. Therefore, for different HTTP protocol versions, the Gzip function needs to be selectively enabled or disabled. This directive is used to set the minimum HTTP protocol version for enabling the Gzip function.
# The default setting is version 1.1, that is, the Gzip function is used to compress the response output data only when the client uses HTTP protocol version 1.1 or above.
# Currently, most browsers support Gzip self-decompression, so the default value is generally acceptable.
zip_http_version 1.0 | 1.1;

# This directive sets the number of bytes of the page. The Gzip function is enabled only when the size of the response page is larger than this value.
# It is recommended to set gzip_min_length to 1024;
gzip_min_length length;

# Used to set whether the Nginx server performs Gzip compression on the results returned by the backend server;
# In general, the backend is used as a restAPI interface, and the amount of data returned is not too large, so compression is not recommended. # If you really need to compress the data returned by the backend, you can check the content of this section gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;

# Set the MIME type. The set type will be compressed. The default value is: text/html
# This variable can also be set to "*", which means Gzip compression of page data of all MIME types. # Generally, it can be set to: gzip_types text/plain application/javascript text/css text/xml
gzip_types mime-type ...;

# The effect of turning it on is adding Accept-Encoding: gzip to the response header
gzip_vary on | off;

ngx_http_gzip_static_module module

The ngx_http_gzip_static_module module is mainly responsible for searching and sending data pre-compressed by the Gzip function. These data are stored on the server with the suffix ".gz". If the data requested by the client has been compressed before and the client browser supports Gzip compression, the compressed data will be returned directly.

The main difference between this module and the ngx_http_gzip_module module is that this module uses static compression and includes the Content-Length header field in the HTTP response header to indicate the length of the message body. It is used when the server can determine the length of the response data; while the latter uses Chunked encoded dynamic compression by default. It is mainly suitable for situations where the server cannot determine the length of the response data, such as downloading large files, and the data length needs to be generated in real time.

The usage of this module's instructions is similar to that of the ngx_http_gzip_static_module module, so we will not go into details here. You can refer to the official documentation

This module is an optional HTTP module for the Nginx server. If you want to use it, you must add the --with-http_gzip_static_module directive when configuring the Nginx program.

ngx_http_gunzip_module module

The Nginx server supports Gzip compression of the response output data stream. For the client browser, it needs to be able to decompress and process Gzip compressed data. However, if the client itself does not support this function, the Nginx server needs to decompress the data before sending it. These compressed data may come from the backend server compression or Nginx server pre-compression. The ngx_http_gunzip_module module is used to decompress compressed data for client browsers that do not support Gzip compressed data processing.

Similarly, the instructions for using this module will not be expanded in detail. You can refer to the official documentation

Modern browsers generally support compression, so this module is less likely to be used.

Configuration Example

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/xml;

In order to enable the Nginx server to apply Gzip compression function globally, you can put the Gzip configuration in the http global block. If we want to treat each virtual host differently, we can
Add the respective Gzip configuration directives in the block;

Read more

Not all applications developed in daily life may use Nginx. Let's see how other web servers can enable support for HTTP compression.

1. Enable compression for Tomcat embedded in Spring Boot

Tomcat, as a servet container + http server, also supports gzip compression. If we use traditional Tomcat, we only need to enable HTTP compression in the server.xml configuration.
In the embed version, it needs to be configured through code. The built-in tomcat of spring-boot is the embed version. Some default tomcat configurations have been made through the built-in autoconfig mechanism, but spring-boot does not provide an entry for some uncommon/advanced configurations.

However, due to the characteristics of spring beans, you can override the default assembled beans, including tomcat-related configurations. Use the TomcatConnectorCustomizer interface to enable compression configuration.

public class ConnC1 implements TomcatConnectorCustomizer {

 @Override
 public void customize(Connector connector) {
  ProtocolHandler protocolHandler = connector.getProtocolHandler();
  if (protocolHandler instanceof Http11NioProtocol) {
   Http11NioProtocol http11NioProtocol = (Http11NioProtocol)protocolHandler;
   http11NioProtocol.setCompression("on");//default off
   http11NioProtocol.setCompressibleMimeType();
   http11NioProtocol.setCompressionMinSize(2048);//default 2048(B)
   http11NioProtocol.setMaxKeepAliveRequests(1);//default 200
  }
 }
}

Regarding Tomcat's support for HTTP compression, you can start looking for clues from Tomcat's CompressionConfig class.

In fact, if you just want to simply enable support for compression, you only need to do the following configuration in Spring Boot:

server:
 compression:
 enabled: true
 min-response-size: 1024
 mime-types:
  application/json

Summarize

This is the end of this article about Nginx's Gzip function. For more information about Nginx's Gzip function, please search 123WORDPRESS.COM's previous articles 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
  • About Nginx gzip configuration
  • Summary of using Nginx gzip command

<<:  Detailed explanation of map overlay in openlayers6

>>:  mysql is not an internal command error solution

Recommend

Detailed explanation of how to synchronize data from MySQL to Elasticsearch

Table of contents 1. Synchronization Principle 2....

Security considerations for Windows server management

Web Server 1. The web server turns off unnecessar...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Example of using CSS3 to create Pikachu animated wallpaper

text OK, next it’s time to show the renderings. O...

MySQL 8.0.23 installation and configuration method graphic tutorial under win10

This article shares the installation and configur...

Detailed process of drawing three-dimensional arrow lines using three.js

Demand: This demand is an urgent need! In a subwa...

Element dynamic routing breadcrumbs implementation example

To master: localStorage, component encapsulation ...

Detailed explanation of the abbreviation of state in react

Preface What is state We all say that React is a ...

How to install and configure the Apache Web server

Learn how to host your own website on Apache, a r...

Implementation steps for building a local web server on Centos8

1 Overview System centos8, use httpd to build a l...

How to change the root user's password in MySQL

Method 1: Use the SET PASSWORD command mysql> ...

A brief discussion on the types of node.js middleware

Table of contents Overview 1. Application-level m...

Use of Linux file command

1. Command Introduction The file command is used ...

Use shell script to install python3.8 environment in CentOS7 (recommended)

One-click execution To install Python 3.8 in a vi...