How to Enable Nginx Gzip Compression (Config Generator)

How to Enable Nginx Gzip Compression (Config Generator)

Nginx gzip compression shrinks text responses before they leave your server, so pages load faster and use less bandwidth. The catch is getting the directives right: the wrong MIME types or a missing setting means assets ship uncompressed. Here is what each directive does and how to build a working block in seconds.

Why enable Nginx gzip

Text-based assets like HTML, CSS, JavaScript, and JSON compress extremely well, often by 70 percent or more. Smaller responses mean quicker downloads, lower transfer costs, and a faster first paint for your users. Gzip handles this on the fly with almost no CPU overhead at sensible compression levels.

The fastest way to get a correct block is the Nginx Gzip Configuration Generator, which assembles the directives and MIME types for you.

The directives that matter

A solid gzip block uses these settings:

  • gzip on: turns compression on. Nothing happens without it.
  • gzip_types: the MIME types to compress, such as text/css, application/javascript, and application/json. Note that text/html is always compressed and does not need to be listed.
  • gzip_comp_level: how hard to compress, from 1 to 9. Levels 4 to 6 are the sweet spot, since higher levels add CPU cost for tiny size gains.
  • gzip_min_length: skip files below this size, often 256 or 1024 bytes. Compressing tiny files can make them larger.
  • gzip_vary on: adds a Vary: Accept-Encoding header so proxies and CDNs cache compressed and uncompressed versions separately.

What not to compress

Do not add already-compressed binaries to gzip_types. Images like JPEG, PNG, and WebP, along with most modern font formats such as WOFF2, are already compressed. Running gzip over them wastes CPU and can slightly inflate the file. Keep your type list to text and text-like formats only.

If you want even smaller payloads, Brotli is a stronger alternative to gzip for static text assets. It typically beats gzip on ratio at comparable speed, though it needs the Brotli module compiled into Nginx. Many teams enable both and let the browser pick.

Generate and apply your config

  1. Open the Nginx Gzip Configuration Generator and choose your MIME types and compression level.
  2. Copy the generated block into your http or server context in nginx.conf.
  3. Test the syntax with nginx -t to catch typos before they take down the server.
  4. Apply it with nginx -s reload, which reloads config without dropping connections.

The generator runs entirely in your browser. Nothing you enter is sent anywhere.

Set the right types, pick level 4 to 6, run nginx -t, reload, and your text assets ship lighter from the next request on.

← All posts