search
HomeOperation and MaintenanceNginxHow to use gzip compression in nginx to improve website speed

Why use gzip compression

Turn on gzip compression of nginx, the size of static resources such as js and css in the web page will be greatly reduced, thereby saving a lot of bandwidth and improving transmission efficiency. , giving users a fast experience.

nginx implements gzip

The principle of nginx to implement resource compression is to intercept requests through the default integrated ngx_http_gzip_module module, and to type that needs to be gzip To make gzip, it is very simple to use, just turn it on and set the options. .

Request headers and response headers after gzip takes effect

request headers:
accept-encoding:gzip,deflate,sdch

response headers:
content-encoding:gzip
cache-control:max-age240

gzip processing process

From the perspective of the http protocol, the request header declares accept- encoding:gzip deflate sdch (referring to the compression algorithm, where sdch is a compression method recommended by Google)
Server->Response->Compress the content with gzip->Send to the browser->Browser decodes gzip ->Receive gzip compressed content

Common configuration parameters of gzip

  • gzip on|off Whether to turn on gzip

  • gzip_buffers 4k Buffers (How many blocks are compressed in the memory buffer? How big is each block?)

  • gzip_comp_level [1-9] Recommended 6 Compression level, the higher the level of compression Minimum, and the more CPU resources are wasted

  • gzip_disable   What kind of uri does the regular match ua look like without gzip

  • gzip_min_length   200 The minimum length to start compression , nginx does not compress it if it is less than this length

  • gzip_http_version 1.0|1.1 HTTP protocol version starting to compress (default 1.1)

  • gzip_proxied Set the request Or a proxy server, how to cache content

  • gzip_types text/plain application/xml What types of files should be compressed, such as txt, xml, html, css

  • gzip_vary off Whether to transmit the gzip compression flag

nginx configuration gzip

Static page index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>演示nginx做gzip压缩</title>
  <script src="./jquery.js" ></script>
</head>
<body>
<img  src="/static/imghwm/default1.png"  data-src="./nginx_img.jpeg"  class="lazy"     style="max-width:90%" / alt="How to use gzip compression in nginx to improve website speed" >
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
<h1 id="nginx实现gzip压缩-减少带宽的占用-同时提升网站速度">nginx实现gzip压缩,减少带宽的占用,同时提升网站速度</h1>
</body>
</html>

nginx configuration

server{
    listen 80;
    server_name localhost 192.168.0.96;
    gzip on;
    gzip_buffers 32 4k;
    gzip_comp_level 6;
    gzip_min_length 200;
    gzip_types application/javascript application/x-javascript text/javascript text/xml text/css;
    gzip_vary off;
    root /users/lidong/desktop/wwwroot/test;
    index index.php index.html index.htm;
    access_log /users/lidong/wwwlogs/access.log;
    error_log /users/lidong/wwwlogs/error.log;
    location ~ [^/]\.php(/|$) {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param script_filename $document_root$fastcgi_script_name;
        include    fastcgi_params;
    }
}

is the page request before using gzip:

How to use gzip compression in nginx to improve website speed

Turns on the gzip page request:

How to use gzip compression in nginx to improve website speed

How to use gzip compression in nginx to improve website speed

Note

  • Pictures and mp3 generally do not need to be compressed because the compression rate is relatively small

  • Generally compress files in text, css, js, and xml formats

  • Small files do not need to be compressed and may be larger than the source files

  • Binary files do not need to be compressed

The above is the detailed content of How to use gzip compression in nginx to improve website speed. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Deploying Applications with NGINX Unit: A GuideDeploying Applications with NGINX Unit: A GuideMay 04, 2025 am 12:03 AM

NGINXUnitischosenfordeployingapplicationsduetoitsflexibility,easeofuse,andabilitytohandledynamicapplications.1)ItsupportsmultipleprogramminglanguageslikePython,PHP,Node.js,andJava.2)Itallowsdynamicreconfigurationwithoutdowntime.3)ItusesJSONforconfigu

NGINX and Web Hosting: Serving Files and Managing TrafficNGINX and Web Hosting: Serving Files and Managing TrafficMay 03, 2025 am 12:14 AM

NGINX can be used to serve files and manage traffic. 1) Configure NGINX service static files: define the listening port and file directory. 2) Implement load balancing and traffic management: Use upstream module and cache policies to optimize performance.

NGINX vs. Apache: Comparing Web Server TechnologiesNGINX vs. Apache: Comparing Web Server TechnologiesMay 02, 2025 am 12:08 AM

NGINX is suitable for handling high concurrency and static content, while Apache is suitable for dynamic content and complex URL rewrites. 1.NGINX adopts an event-driven model, suitable for high concurrency. 2. Apache uses process or thread model, which is suitable for dynamic content. 3. NGINX configuration is simple, Apache configuration is complex but more flexible.

NGINX and Apache: Deployment and ConfigurationNGINX and Apache: Deployment and ConfigurationMay 01, 2025 am 12:08 AM

NGINX and Apache each have their own advantages, and the choice depends on the specific needs. 1.NGINX is suitable for high concurrency, with simple deployment, and configuration examples include virtual hosts and reverse proxy. 2. Apache is suitable for complex configurations and is equally simple to deploy. Configuration examples include virtual hosts and URL rewrites.

NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function