Mahdi.Kh
September 21, 2026
By compressing responses, the amount of data sent is reduced, and transferring it over the network happens faster.
Compressing responses reduces the size of files like HTML, CSS, JavaScript, and JSON by around 70% or more! After compression, because the data is smaller, it's transferred over the network faster, which speeds up how quickly the website loads.
Also, by reducing the amount of data sent, bandwidth usage goes down, and if costs are calculated based on traffic, server costs go down as well.
One of the algorithms we can use for compression is called gzip. To enable it, you can use the gzip on directive. The gzip_types directive specifies that compression should only be applied to responses whose content type (Content-Type) matches the specified values.
gzip_vary Directive For?When gzip is enabled, nginx compresses responses before sending them to the user's browser, so the data size is reduced and the transfer happens faster. But not all browsers or clients may support compression.
For the server to know whether the browser is able to decompress a compressed file or not, the browser sends the Accept-Encoding header with its request, which specifies whether it supports gzip, for example, or not!
The problem arises when a user with a browser that supports gzip opens a page, and a caching service or CDN stores the compressed version of that page. Then another user, with a browser that doesn't support gzip, wants to view the same page. Because the server or cache can't tell the difference between these two users, it sends the compressed version to the second user as well. As a result, that user's browser can't decompress the file, and the page isn't displayed correctly.
To prevent this problem, caches need to be told that responses may differ based on the Accept-Encoding header. This is done by adding the Vary: Accept-Encoding header to responses.
By enabling the gzip_vary on directive, the server automatically adds this header to compressed responses. This lets caches know that responses will differ depending on the client's Accept-Encoding header value. So the cache keeps the compressed version only for clients that accept gzip, and sends the regular version to everyone else.
As a result, the gzip_vary on directive plays an important role in ensuring compressed files are only sent to browsers that support them, and it lets every user view the site's pages without errors or issues.
You can access the files related to this part on GitHub.
Previous part: Blocking and Whitelisting IPs | Nginx from Scratch
Next part: Location Modifiers | Nginx from Scratch