Operations 5 min read

How to Enable and Optimize Gzip Compression in Nginx for Faster Websites

This guide explains how to activate gzip compression in Nginx, configure its compression level, buffer size, and minimum file size, and apply it to both static and dynamic responses, providing step‑by‑step examples to improve web performance.

Raymond Ops
Raymond Ops
Raymond Ops
How to Enable and Optimize Gzip Compression in Nginx for Faster Websites
Nginx

is a high‑performance web server that can also act as a reverse proxy and load balancer. Using

gzip

compression and decompression can significantly reduce the size of transferred files and speed up website access.

1. Enable gzip compression

First, enable

gzip

in the Nginx configuration file, typically located at

/etc/nginx/nginx.conf

. Find the

http

block and add the following directives:

<code>http {
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/javascript;
    ...
}</code>
gzip on

: turns on gzip compression.

gzip_disable "msie6"

: disables gzip for the outdated MSIE6 browser.

gzip_types

: specifies the MIME types that should be compressed.

2. Configure gzip compression level

Set the compression level (1‑9) to balance compression ratio and CPU usage. Example configuration:

<code>http {
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/javascript;
    gzip_comp_level 6;
    ...
}</code>

The

gzip_comp_level

directive accepts values from

1

(fastest, lowest compression) to

9

(slowest, highest compression). In the example, level

6

is used.

3. Configure gzip buffers

Define the size of the compression buffers. Example:

<code>http {
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/javascript;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    ...
}</code>

The

gzip_buffers

directive allocates

16

buffers of

8k

each. Nginx will automatically adjust buffers if needed, but this setting allows fine‑tuning.

4. Set minimum file size for compression

Specify the smallest file size that will be compressed. Example:

<code>http {
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/javascript;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_min_length 256;
    ...
}</code>

Only files larger than

256

bytes will be processed by gzip.

5. Use gzip for dynamic content

After the above settings, Nginx will automatically compress suitable responses. To enable compression for proxied or dynamically generated content, add the following in a

location

block:

<code>location / {
    ...
    gzip_proxied any;
    ...
}</code>

The

gzip_proxied any

directive applies gzip compression to all proxied requests.

By configuring these directives, you can flexibly use Nginx's gzip compression and decompression features to optimize website speed, including both static files and dynamically generated responses.

web performanceNginxgzipServer Configurationcompression
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.