Complete steps to enable gzip compression in nginx

Complete steps to enable gzip compression in nginx

Preface

Enabling gzip compression on a website is the most common way to increase access speed on a website. It increases the access speed of a website by compressing static resources.

1. Configure gzip compression

  • Open the nginx configuration file
  • Modify the gzip configuration in nginx
  • Order
 vim /etc/nginx/nginx.conf

run

2. Detailed configuration

A detailed explanation of gzip configuration is as follows

 #Whether to enable gzip compression, on means enable, off means enable gzip on;
 
#Common static resources that need to be compressed 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;
 
#Since nginx compression occurs on the browser side and Microsoft's IE6 is very bad, it will cause the compressed image to be invisible, so this option is to disable IE6 from compression gzip_disable "MSIE [1-6]\.";
 
#If the file is larger than 1k, start compression gzip_min_length 1k;
 
#Use 16k as a unit, and apply for memory space in 4 times the size of the original data. Generally, do not modify gzip_buffers 4 16k;
 
#Compression level, the number selection range is 1-9, the smaller the number, the faster the compression speed, and the more CPU consumption gzip_comp_level 2;
 
#Include all sub-configuration files with the suffix .conf in the /etc/nginx/conf.d directory include /etc/nginx/conf.d/*.conf;

3. Restart nginx service

For the configuration to take effect, remember to restart the nginx service

 nginx -t
 
nginx -s reload

4. Is the activation successful?

The first way to check whether a website uses gzip compression is to use the following command

Order

 curl -I -H "Accept-Encoding:gzip,deflate" "URL you want to view"

If the following result appears, the startup is successful

 Content-Encoding:gzip

The second way to check whether a website uses gzip compression is to check the Content-Encoding option in the response header through the console. If gzip appears, it is enabled successfully.

Although Nginx's Gzip compression function is easy to use, it is not recommended to enable this compression function for the following two types of file resources.

1) Image type resources (and video files)

Reason: Pictures such as jpg and png files are compressed themselves, so even if gzip is turned on, there is not much difference in size before and after compression, so turning it on will waste resources. (You can try compressing a jpg image into zip and observe that the size does not change much. Although the zip and gzip algorithms are different, it can be seen that the value of compressing images is not great)

2) Large file resources

Reason: It will consume a lot of CPU resources and may not have obvious effects.

Summarize

This is the end of this article about enabling gzip compression in nginx. For more information about enabling gzip compression in nginx, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. 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
  • Detailed explanation of gzip configuration parameters under nginx
  • How to enable Gzip compression in Nginx to significantly increase page loading speed
  • Detailed explanation of how to enable Gzip compression in Nginx server configuration
  • Detailed explanation of GZip configuration parameters in Nginx server
  • Nginx enables GZIP compression web page transmission method (recommended)
  • nginx configure gzip compressed page
  • Speed ​​up nginx performance: enable gzip and cache
  • Nginx Basics - Gzip Configuration Guide
  • An article to understand the gzip function of nginx

<<:  Detailed explanation of computed properties in Vue

>>:  CSS3 implements the sample code of NES game console

Recommend

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

MySQL detailed summary of commonly used functions

Table of contents MySQL Common Functions 1. Numer...

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

Build a Scala environment under Linux and write a simple Scala program

It is very simple to install Scala environment in...

Detailed explanation of pipeline and valve in tomcat pipeline mode

Preface In a relatively complex large system, if ...

Detailed explanation of Docker Swarm service orchestration commands

1. Introduction Docker has an orchestration tool ...

Vue3.x uses mitt.js for component communication

Table of contents Quick Start How to use Core Pri...

React Diff Principle In-depth Analysis

Table of contents Diffing Algorithm Layer-by-laye...

How to install MySQL database on Ubuntu

Ubuntu is a free and open source desktop PC opera...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Vue implements setting multiple countdowns at the same time

This article example shares the specific code of ...

20 Signposts on the Road to Becoming an Excellent UI (User Interface) Designer

Introduction: Interface designer Joshua Porter pub...

MySQL 5.7.23 decompression version installation tutorial with pictures and text

Download the MySQL installer Official download ad...

Summary of webpack's mobile adaptation solution

Table of contents rem vw Adapt to third-party UI ...

Three ways to configure JNDI data source in Tomcat

In my past work, the development server was gener...