How to Enable Gzip Compression in Nginx
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:
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.
With Gzip enabled:
The browser automatically decompresses the content after receiving it.
Check Whether Gzip Is Already Enabled
Display the current Nginx configuration.
If Gzip has already been configured, you will see several related directives.
Edit the Nginx Configuration
Open the main configuration file.
Inside the http block, add or update the following configuration.
Configuration Explained
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
Compressing these formats again usually wastes CPU time without reducing file size.
Test the Configuration
Verify the Nginx configuration.
Expected output:
syntax is ok
test is successful
Restart Nginx.
Verify That Gzip Is Working
Use curl to inspect the response headers.
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
› 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