How to Enable Gzip Compression in Nginx

Published: 2026-07-06
Google AdSense 广告位 (待申请通过后开启)

Introduction

Gzip compression is one of the simplest ways to improve website performance. By compressing HTML, CSS, JavaScript, JSON, XML, and other text-based resources before sending them to the client, Nginx can significantly reduce the amount of data transferred over the network.

Smaller responses mean faster page loads, lower bandwidth usage, and a better experience for users, especially on slower network connections.

This guide explains how to enable Gzip compression in Nginx, configure the recommended settings, verify that compression is working, and avoid common configuration mistakes.

Test Environment

This tutorial was tested using the following environment:

Operating System : AlmaLinux 10
Web Server       : Nginx
Architecture     : x86_64
Last Tested      : July 2026

What Is Gzip Compression?

Gzip is a compression algorithm that reduces the size of text-based files before they are transmitted to a web browser.

Without compression, every request transfers the original file.

Browser
    │
    ▼
Nginx
    │
Original HTML (120 KB)
    │
    ▼
Browser

With Gzip enabled:

Browser
    │
    ▼
Nginx
    │
Compressed HTML (25 KB)
    │
    ▼
Browser

The browser automatically decompresses the content after receiving it.

Check Whether Gzip Is Already Enabled

Display the current Nginx configuration.

nginx -T | grep gzip

If Gzip has already been configured, you will see several related directives.

Edit the Nginx Configuration

Open the main configuration file.

sudo nano /etc/nginx/nginx.conf

Inside the http block, add or update the following configuration.

gzip on;

gzip_comp_level 5;

gzip_min_length 1024;

gzip_vary on;

gzip_proxied any;

gzip_types
    text/plain
    text/css
    text/xml
    text/javascript
    application/javascript
    application/json
    application/xml
    application/rss+xml
    application/xhtml+xml
    application/xml+rss
    image/svg+xml
    font/ttf
    font/otf
    application/vnd.ms-fontobject
    application/font-woff
    application/font-woff2;

Configuration Explained

gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied any;

gzip on

Enables Gzip compression.

gzip_comp_level

Defines the compression level.

1 = fastest

9 = highest compression

A value between 4 and 6 provides an excellent balance between CPU usage and compression ratio.

gzip_min_length

Only compress responses larger than 1 KB.

Compressing extremely small files usually provides little benefit.

gzip_vary

Adds the Vary: Accept-Encoding response header.

This allows caches and CDNs to correctly distinguish between compressed and uncompressed responses.

gzip_proxied

Enables compression for requests that pass through reverse proxies.

Enables Gzip compression.

gzip_types

Specifies which MIME types should be compressed.

Text-based resources benefit the most from Gzip.

Avoid compressing files that are already compressed, such as:

JPEG

PNG

GIF

MP4

ZIP

PDF

Compressing these formats again usually wastes CPU time without reducing file size.

Test the Configuration

Verify the Nginx configuration.

sudo nginx -t

Expected output:

syntax is ok

test is successful

Restart Nginx.

sudo systemctl restart nginx

Verify That Gzip Is Working

Use curl to inspect the response headers.

curl -I -H "Accept-Encoding: gzip" https://example.com

You should see:

Content-Encoding: gzip

This confirms that Nginx is sending compressed content.

You can also verify compression using your browser's Developer Tools by checking the Content-Encoding response header.

Common Issues

Gzip Is Not Working

Possible causes include:

The configuration was added outside the http block.

Nginx has not been restarted.

The requested file type is not included in gzip_types.

The response is smaller than gzip_min_length.

Images Are Not Smaller

This is expected.

Image formats such as JPEG and PNG are already compressed.

Gzip is designed for text-based content rather than binary files.

High CPU Usage

If CPU utilization becomes unusually high, reduce the compression level.

gzip_comp_level 4;

Higher compression levels require more CPU time while often producing only modest improvements in file size.

Best Practices

Enable Gzip for all production websites.

Use a compression level between 4 and 6.

Compress only text-based resources.

Do not compress files that are already compressed.

Always verify the configuration after making changes.

Conclusion

Gzip compression is an easy and effective way to improve website performance. By reducing the size of HTML, CSS, JavaScript, JSON, and other text-based resources, Nginx can decrease bandwidth usage and deliver faster page loads.

Combined with PHP OPcache, PHP-FPM optimization, and Nginx FastCGI Cache, Gzip forms an important part of a high-performance PHP web server.

Explore More

Technology Guides →

How to Enable PHP OPcache for Better Performance | Complete Guide

How to Configure Nginx FastCGI Cache for Better PHP Performance

How to Configure Nginx Reverse Proxy on AlmaLinux 10

How to Install Nginx on AlmaLinux 10

Southeast Asia Insights →