Gzip
Gzip is a widely used lossless data compression method, employed on the web mainly to shrink the size of files sent between a server and the user's browser. When a browser requests a page, the server can compress text files on the fly — HTML, CSS and JavaScript — before sending them over the wire. The result is often several times less data transferred and a noticeably faster site, which is why enabling Gzip is a fundamental step in performance optimization.
How Gzip works
The mechanism relies on negotiation between client and server within the HTTP protocol:
- The browser signals with an Accept-Encoding: gzip header that it accepts compressed content.
- The server compresses the response using the DEFLATE algorithm (combining LZ77 and Huffman coding) and adds a Content-Encoding: gzip header.
- The browser receives the smaller payload and decompresses it on its side before rendering the page.
Compression is especially effective for text files with repeating patterns (tags, property names, keywords). There is no point compressing already-compressed formats like JPEG, PNG or MP4 — the gain would be negligible while wasting CPU.
Gzip in practice
Gzip is enabled in the web server configuration:
- Apache — via the mod_deflate module, often configured in the .htaccess file.
- Nginx — with the gzip on and gzip_types directives specifying which file types to compress.
- CDN and cache — content delivery networks usually compress responses automatically.
A good practice is to combine Gzip (or the more modern Brotli) with Cache-Control headers, so you shrink transfer size and reduce the number of downloads at once. A correct configuration is easy to verify in the browser's developer tools by checking for the Content-Encoding header.
Powiązane pojęcia
Najczęstsze pytania
How is Gzip different from Brotli?
Both are lossless compression algorithms for HTTP traffic. Brotli, developed by Google, usually offers a slightly better compression ratio for text files at comparable performance, so it is often the first choice for modern sites. Gzip, however, remains a universal standard supported almost everywhere and an excellent fallback.
