gzip
The gzip directive enables or disables gzip compression in NGINX.
Description
The 'gzip' directive in NGINX's HTTP Core module is used to control gzip compression for responses sent to clients. By enabling this directive, NGINX will compress the responses using the gzip algorithm, which can significantly reduce the size of the transmitted data and improve loading times for clients. The server checks the permitted content types and client capabilities (such as the presence of the 'Accept-Encoding: gzip' header) to determine whether to respond with compressed data.
This directive can take a boolean value: when set to 'on', gzip compression is enabled, while 'off' disables it. Additionally, NGINX provides several configuration parameters that can further refine the behavior of gzip compression, such as 'gzip_types', which specifies the MIME types of files to compress, and 'gzip_vary', which indicates whether to add the Vary header to responses to signify that a different version exists for clients that do not support gzip. The use of gzip can be beneficial for reducing bandwidth costs and enhancing user-experience, especially for text-based files like HTML, CSS, and JavaScript.
It is important to ensure that gzip compression is utilized judiciously, as not all content types benefit from compression, and excessive use can introduce unnecessary CPU overhead on the server.
Config Example
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}Make sure the gzip module is included during NGINX compilation, as it may be excluded in certain builds.
Excessively compressing already compressed files (like JPEG images) will not provide any benefit and may even increase file size.
Remember to check client compatibility; some clients may not support gzip, and responses will not be compressed for them.