gunzip
The gunzip directive enables or disables the decompression of gzip-compressed responses in NGINX.
Description
The gunzip directive is utilized in NGINX to control whether the server should automatically decompress gzip-compressed HTTP response bodies before sending them to the client. When enabled, NGINX will uncompress the content, allowing clients that do not support gzip compression to properly interpret the response without any additional processing on their side. This directive is important for ensuring compatibility with various clients that may not be able to handle gzip encoding properly.
The gunzip directive accepts a flag as its argument, where on enables the decompression, and off disables it. This directive can be used at the http, server, and location contexts, allowing for fine-grained control over when responses are decompressed based on different configurations. It is crucial to use this directive judiciously; for example, turning it on for certain locations that serve resources that may be compressed can reduce bandwidth and improve load times, but may introduce overhead for very small files.
As NGINX uses the Content-Encoding header to determine whether a response is compressed, the gunzip directive works in tandem with gzip-related directives that control the compression process. If a response is not compressed (i.e., it does not include the Content-Encoding: gzip header), the gunzip setting will have no effect, meaning that the original content will be sent as-is, regardless of the directive's state. Users must ensure that when enabling gunzip, the upstream server or application is correctly setting the headers for compression to take full effect.
Config Example
server {
listen 80;
server_name example.com;
location / {
gunzip on;
proxy_pass http://backend;
}
}Ensure that the upstream server sends the correct Content-Encoding header; otherwise, gunzip will have no effect.
Do not use gunzip with weak network connections, as decompression may add CPU overhead leading to performance issues.
Avoid enabling gunzip for particularly small files, as the overhead of decompression may negate any benefit from bandwidth savings.