21+ years in hosting · Jaipur, India · INR billing with 18% GST · 24/7 email support
Blog · WebJi® India

Enable Gzip Compression on Nginx

Quick answer

Enable Gzip compression on Nginx by setting gzip on in an appropriate http, server or location block and adding CSS, JavaScript and other text types to gzip_types. Test with nginx -t before reloading, then request a GET response accepting gzip and confirm Content-Encoding: gzip. Review sensitive endpoints separately because compressed TLS responses can expose secrets.

For administrators of their own Nginx server who need correct directive placement, safe reloads and an observable compression result.

✓ In business since 2005
✓ INR + GST billing
✓ 24/7 Email Support — avg reply < 60 min
GuideWebJi®
knowledge
Read time
5 min
Sections
7
Updated
14 Nov 2026
Written by
WebJi® team

What Gzip compression does on Nginx

Gzip compression on Nginx reduces the bytes sent for text responses by applying the ngx_http_gzip_module filter when a client accepts gzip. The browser decompresses the response before rendering, so the content stays the same.

Stock Nginx defaults to gzip off;. Once enabled, text/html is included automatically; other types must be named with gzip_types. This distinction explains why a site can compress its homepage but leave CSS and JavaScript unchanged.

The official Nginx gzip module documentation lists the defaults and valid contexts. These instructions apply to a server you can configure, such as a Linux VPS, not an ordinary shared account where server configuration is controlled by the host.

Which Nginx directives enable Gzip compression?

Set gzip on, enable Vary: Accept-Encoding, choose a compression level and add the text MIME types your public site serves. Use this starting configuration inside the existing http block or a selected server block, without duplicating directives already there.

gzip on;
gzip_vary on;
gzip_comp_level 5;
gzip_min_length 1000;
gzip_types text/plain text/css text/javascript application/javascript application/json application/xml image/svg+xml;
Nginx Gzip settings: documented defaults and this example
DirectiveDefaultExample purpose
gzipoffEnable response compression
gzip_varyoffTell caches responses vary by accepted encoding
gzip_comp_level15 is a starting compromise, not a universal optimum
gzip_min_length20 bytes1000 skips small responses with a known Content-Length
gzip_typestext/htmlAdd CSS, JavaScript, JSON, XML and SVG

Do not repeat text/html in gzip_types: it is already included. Leave out JPEG, PNG, WebP, WOFF2, MP4 and ZIP because those formats are already compressed. Level 9 uses more processing for compression; measure CPU and transferred bytes before choosing it.

Where should the Gzip configuration go in Nginx?

Put the Gzip configuration in http for a global setting, server for one site, or location for a narrower scope. The location should match the responses you intend to compress, not unrelated private endpoints.

The main configuration is commonly /etc/nginx/nginx.conf. Distribution packages may include site files from /etc/nginx/sites-enabled/ or /etc/nginx/conf.d/. Inspect your actual include structure rather than assuming either path is always active.

A more specific block can override an inherited setting, while repeating the same directive in one context can produce a duplicate-directive error. It is not generally true that the last included file wins. On a managed panel, use its supported custom-directive mechanism so generated updates do not overwrite the change.

Nginx does not read .htaccess. The .htaccess guide applies to Apache-compatible configurations, not this filter. If Apache is behind Nginx, identify which layer sends the public response before changing either one.

How to test and reload Nginx after enabling Gzip

Run nginx -t before reloading, and reload only after the configuration test succeeds. A syntax test protects the running service from a malformed new configuration; it does not prove that a particular response will be compressed.

sudo nginx -t
sudo systemctl reload nginx

The second command applies to a systemd installation. Where systemd is not used, reload the existing Nginx master with:

sudo nginx -s reload
  1. Keep a copy of the configuration you are replacing.
  2. Run the test and resolve the file and line it reports.
  3. Reload using the command appropriate to the installation.
  4. Check one HTML page and one CSS or JavaScript response.
  5. If behaviour is wrong, restore the previous configuration, test it and reload again.

Missing semicolons and directives in the wrong context are common causes of failure. Confirm MIME types from the response headers too: a valid config can still omit the type your application actually emits. If the site also needs HTTPS, Certbot on Nginx handles certificates separately.

How do you verify that Nginx sends Gzip?

Request a real GET response with Accept-Encoding: gzip and look for Content-Encoding: gzip on that response. Use a representative public URL and repeat the test for a text asset.

curl -sS -D - -o /dev/null -H "Accept-Encoding: gzip" https://example.com/
curl -sS -D - -o /dev/null -H "Accept-Encoding: gzip" https://example.com/assets/site.css

Replace the example domain and asset path with your own URLs. This GET-based check is preferable to relying only on curl -I, which sends HEAD and may not exercise response-body filters the same way.

  • Content-Encoding: gzip: that response was compressed.
  • Vary: Accept-Encoding: intermediaries should distinguish compressed and uncompressed variants.
  • No encoding on a tiny response: check Content-Length against gzip_min_length.
  • HTML passes, CSS fails: inspect Content-Type and the gzip_types list.
  • CDN involved: the public header may describe CDN compression, not the origin server.

The Nginx $gzip_ratio variable can record original size divided by compressed size in logs. No fixed percentage saving is guaranteed: payload structure, size and existing compression all affect the result.

How proxies, Brotli and sensitive pages affect Nginx Gzip

Proxy handling, optional Brotli modules and sensitive responses require separate decisions; enabling Gzip alone does not configure them safely. Start with public text resources and review authenticated pages before extending the scope.

gzip_proxied defaults to off and uses the request's Via header to recognise proxied requests. The documented choices include:

gzip_proxied expired no-cache no-store private auth;

That line enables compression for matching proxied responses; it is not a security control. gzip_proxied any; is broader still. Do not add either blindly to a server containing sensitive pages.

Nginx warns that TLS-compressed responses can be exposed to BREACH-style attacks when secrets and attacker-controlled input appear together. Review those endpoints and disable compression where appropriate. An existing location serving such a response can use gzip off;; the application's routes determine the correct scope.

Brotli needs an additional module such as ngx_brotli; installing stock Nginx does not guarantee it is available. Clients advertise supported encodings and should receive one encoded response, not nested compression. The Apache versus Nginx comparison covers the server choice, while Redis object caching for WordPress reduces database work rather than transferred response bytes.

FAQ

Nginx Gzip questions

Why does nginx -t pass but the response stay uncompressed?

The configuration may be valid while the request does not qualify. Check Accept-Encoding, Content-Type, response size, an inherited gzip off setting and any Via header. Also confirm that the tested URL reaches the server block you changed.

Does Gzip reduce slow database queries?

No. Gzip reduces the body transferred after the response is generated. It does not make SQL queries faster or repair slow application code. Measure server processing time separately from download size before deciding which optimisation is needed.

Is gzip_static the same as gzip on?

No. gzip_static serves a prepared .gz file when the module is available and the corresponding file exists. gzip on compresses suitable responses during processing. Generating static assets in advance can reduce repeated compression work, but it is a separate deployment step.

Can I enable this from a shared cPanel account?

Not by editing Nginx's server configuration. Shared accounts do not normally grant that access. Ask the host which server and compression controls the account uses, or make the change on your own VPS with a tested configuration and reload procedure.

Directives checked against Nginx documentation on 14 November 2026 · Reviewed by the WebJi® team, Jaipur.

WebJi® since 2005

Ready to get started with WebJi®?

Join 12,000+ businesses across India and overseas. INR pricing, 18% GST shown before payment and 24/7 email support in Hindi and English — average first reply under 60 minutes.