What is Gzip
Gzip is a data compression algorithm widely used to compress HTTP responses. When a server sends HTML, CSS, JS, or JSON files, it can compress them with gzip before sending them to the browser. The browser automatically decompresses the data.
Gzip significantly reduces the amount of data transferred and speeds up page loading.
How effective is Gzip
The compression ratio depends on the file type:
| File type | Uncompressed | With Gzip | Savings |
|---|---|---|---|
| HTML | 100 KB | 25 KB | 75% |
| CSS | 80 KB | 15 KB | 81% |
| JavaScript | 200 KB | 60 KB | 70% |
| JSON | 50 KB | 10 KB | 80% |
| Images | Already compressed | — | ~0% |
Images (JPEG, PNG, WebP) already use their own compression, so additional gzip compression has no effect.
How the browser requests compression
Запрос браузера:
GET /page HTTP/1.1
Accept-Encoding: gzip, deflate, br
Ответ сервера:
HTTP/1.1 200 OK
Content-Encoding: gzip
Content-Type: text/htmlbr is Brotli, a more efficient compression algorithm (see below).
How to enable Gzip
Nginx
http {
gzip on;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
image/svg+xml;
gzip_proxied any;
gzip_vary on;
}Apache (.htaccess)
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>Node.js / Express
const compression = require('compression');
app.use(compression({
level: 6,
threshold: 1024 // не сжимать файлы < 1KB
}));Next.js
Next.js enables gzip compression by default. For additional configuration:
// next.config.js
module.exports = {
compress: true, // по умолчанию true
}Gzip vs Brotli
| Aspect | Gzip | Brotli |
|---|---|---|
| Compression ratio | Good | 20-30% better |
| Support | All browsers | All modern browsers |
| CPU load | Low | Higher (at the maximum level) |
| Recommendation | For compatibility | Preferred |
Check compression on reChecker
Use HTTP/2 & Brotli to check whether gzip and Brotli are enabled on your website. The tool checks:
- Whether the
Content-Encoding: gziporbrheader is present - Actual compression for common file types
- The HTTP/2 protocol and other performance parameters
FAQ
Does Gzip compress images? Technically, yes, but the effect is minimal—JPEG, PNG, and WebP already use their own compression. Apply gzip only to text formats: HTML, CSS, JS, JSON, XML, and SVG.
Should files smaller than 1 KB be compressed? No. The overhead from compression headers and processing time does not justify compressing small files. The minimum threshold is 1-2 KB.
Does the compression level affect server speed? Yes. Level 1 is fast but compresses less effectively. Level 9 provides maximum compression but puts more load on the CPU. Level 5-6 offers the best balance for most websites.