Nginx improves access speed based on gzip compression

Nginx improves access speed based on gzip compression

1. Why does nginx use gzip?

1. The role of compression:

After the page is compressed using gzip,

The page size can be compressed to about 1/7 of the original size.

The transmission speed and page opening time can be greatly improved.

It is conducive to improving the user's page access experience

2. The principle of Nginx implementing gzip compression:

Intercept requests through the ngx_http_gzip_module module.

And perform gzip compression on the types that need gzip

3. ngx_http_gzip_module is integrated by default in Nginx.

No need to recompile, just turn it on

Source code: https://github.com/liuhongdi/

2. Example of using gzip module

gzip on;
 gzip_min_length 1k;
 gzip_disable "MSIE [1-6]\.";
 gzip_comp_level 5;
 gzip_types text/css text/javascript text/xml text/plain application/json application/javascript application/x-javascript application/xml application/xml+rss;
 gzip_http_version 1.1;

Description of each instruction:

gzip on;

Enable gzip mode,

The default value is off, which means gzip compression is not enabled.

Of course, you have to set it to on.

gzip_min_length 1k;

Set the minimum number of bytes allowed for compressed pages. The number of bytes for a page is obtained from the Content-Length in the header.

The compression effect of files that are too small is not obvious, and the files may even be larger than without compression.

The default value is 20, which is too small.

Generally set the length to be no less than 1k

gzip_disable

IE6 has poor support for Gzip, so it is not enabled for it.

gzip_comp_level :

gzip compression level, 1-9,

The larger the number, the higher the compression ratio, but it also takes more CPU time.

Its default value is 1, and it is usually set to 5 or 6

If the CPU usage is high, setting it to 2 or 3 is also OK

gzip_http_version :

Set the minimum HTTP version supported by gzip compression.

If the version is lower than this, gzip compression is not used.

The default value is 1.1

1.1 is generally used.

gzip_types :

For which types of pages gzip compression is enabled, multiple values ​​are separated by spaces.

The wildcard * can match any type, but don't use this value.

Only the compression of js/css/html/text content has a significant effect.

Enabling compression on files such as pictures and videos that have already been processed by compression algorithms has no obvious effect.

1. Regardless of whether the text/html type is specified, this type of response always enables compression.

So this value does not need to be written

2. The mime-types corresponding to these file types can be found in the nginx/conf/mime.types file

3. 9 commonly used values:

text/css text/javascript text/xml text/plain

application/json application/javascript application/x-javascript

application/xml application/xml+rss

3. Other settings not used in the example:

gzip_buffers 4 16k;

gzip_vary on;

gzip_proxied any;

gzip_vary:

Vary header support,

If on, "Vary: Accept-Encoding" will be added to the Header

This value allows the front-end cache server to cache GZIP compressed pages.

For example, use Squid to cache data compressed by nginx

If you use squid or varnish in front of nginx,

You can set this value to on

gzip_proxied

Enable this option when nginx is used as a front-end proxy.

If the value is any, it means no matter what information the backend server headers return,

Compression is enabled unconditionally

If you use nginx as a reverse proxy,

Set this value to any

gzip_buffers:

Sets the buffer size used to handle compression.

Default: 32 4k|16 8k

32 4k means that the response data is requested in memory in units of 4k at a size of 32 times 4k (128k).

It is recommended not to set this option and use the default value.

4. Check the compression effect of gzip

1. Use curl to view

#-I, --head Show document info only Display only header information

#-H,--header LINE: Pass custom header LINE to server (H) Custom request header information

[root@localhost curl]# curl -I -H "Accept-Encoding: gzip, deflate" http://www.lhdtest.net
HTTP/1.1 200 OK
Server: openresty
Date: Mon, 27 Apr 2020 07:59:38 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: SESSID_WEB=0ks2eh03vmv4dhje29ggojl1bs; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Encoding: gzip

Note: If you see Content_Encoding: gzip, it means gzip compression has taken effect.

2. View the effect through the browser

Check whether gzip is effective from the browser response header information:

See screenshots: Comparison of the effects before and after compression:

Before compression:

After compression:

Note: I use the web console of Firefox. You can also try plugins like yslow.

5. Check the version of nginx:

[root@centos8 soft]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v
nginx version: nginx/1.18.0

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • nginx configure gzip compressed page
  • Detailed explanation of Nginx Gzip module enablement and configuration instructions
  • Detailed explanation of gzip configuration parameters under nginx
  • Use gzip_http_version in Nginx to solve the problem that CDN only supports http 1.0
  • Introduction to Gzip Compression Configuration in Nginx
  • Detailed explanation of GZip configuration parameters in Nginx server
  • Nginx server configuration analysis using gzip compression
  • Detailed explanation of how to enable Gzip compression in Nginx server configuration
  • Tutorial on configuring GZip compression when using Nginx as a reverse proxy for Node.js sites
  • Nginx enables GZIP compression web page transmission method (recommended)
  • Speed ​​up nginx performance: enable gzip and cache
  • Nginx Basics - Gzip Configuration Guide
  • Example of how to enable gzip compression in Nginx
  • How to enable compression and gzip compression in Nginx
  • How to enable Gzip compression in Nginx to significantly increase page loading speed
  • Nginx uses the Gzip algorithm to compress messages
  • About Nginx gzip configuration
  • Detailed explanation of Gzip compression settings for Nginx performance optimization (maximum page opening speed)

<<:  Summary of using the reduce() method in JS

>>:  MySQL case when usage example analysis

Recommend

vue-amap installation and usage steps

I have previously shared the usage of asynchronou...

Detailed process of Vue front-end packaging

Table of contents 1. Add packaging command 2. Run...

Vue implements a simple magnifying glass effect

This article example shares the specific code of ...

MySQL password is correct but cannot log in locally -1045

MySQL password is correct but cannot log in local...

Nginx implements dynamic and static separation example explanation

In order to speed up the parsing of the website, ...

Hbase Getting Started

1. HBase Overview 1.1 What is HBase HBase is a No...

In-depth analysis of JDBC and MySQL temporary tablespace

background Temporary tablespaces are used to mana...

Nginx/Httpd reverse proxy tomcat configuration tutorial

In the previous blog, we learned about the usage ...

How to use crontab to add scheduled tasks in Linux

Preface The Linux system is controlled by the sys...

Use scripts to package and upload Docker images with one click

The author has been working on a micro-frontend p...

Analysis of the HTML writing style and reasons of experienced people

1. Navigation: Unordered List vs. Other Label Ele...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...