
Gzip and Brotli compression shrink text files such as HTML, CSS, JavaScript, JSON and SVG before the server sends them, often by 70 to 90 percent. The browser decompresses them instantly, so pages download faster, especially on mobile data. Brotli usually compresses text 15 to 25 percent smaller than Gzip and is supported by every modern browser over HTTPS. Most good hosts and CDNs enable it automatically; if yours does not, you can turn it on in .htaccess, Nginx or Cloudflare in a few minutes. This guide shows how to check, enable and test compression. This guide explains how to enable Gzip compression step by step.
- Compress text files (HTML, CSS, JS, JSON, SVG, XML, fonts like TTF). Do not re-compress JPG, PNG, WebP, MP4 or WOFF2.
- Prefer Brotli, with Gzip as the fallback for clients that do not accept Brotli.
- Check with
curlor DevTools: the response should showcontent-encoding: brorgzip. - If you use Cloudflare, compression to visitors is handled at the edge automatically.
Enable Gzip compression: how HTTP compression works
The browser tells the server which formats it understands in a request header:
Accept-Encoding: gzip, deflate, br, zstd
Also, the server picks one, compresses the response and labels it:
Content-Encoding: br
Vary: Accept-Encoding
The Vary header tells caches and CDNs to store separate copies for clients that accept different encodings.
Enable Gzip compression: how much it saves
| File | Uncompressed | Gzip | Brotli |
|---|---|---|---|
| HTML page | 120 KB | ~24 KB | ~20 KB |
| Main CSS | 180 KB | ~30 KB | ~25 KB |
| JavaScript bundle | 400 KB | ~120 KB | ~100 KB |
Figures are typical examples; exact savings depend on the content. Text with lots of repetition, such as page-builder HTML and CSS, compresses especially well.
Enable Gzip compression: how to check if compression is on
From the command line:
curl -sI -H "Accept-Encoding: br,gzip" https://www.example.com/ | grep -i content-encoding
curl -sI -H "Accept-Encoding: br,gzip" https://www.example.com/css/style.css | grep -i content-encoding
In Chrome DevTools → Network, add the "Content-Encoding" column (right-click the column headers → Response Headers). PageSpeed Insights also flags "Enable text compression" if it is missing.
Enable Gzip compression: enable Gzip and Brotli on Apache or LiteSpeed (.htaccess)
Many shared hosts in Pakistan run LiteSpeed, which compresses automatically. On Apache, add this to .htaccess:
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
AddOutputFilterByType BROTLI_COMPRESS application/javascript application/json application/xml
AddOutputFilterByType BROTLI_COMPRESS image/svg+xml font/ttf application/vnd.ms-fontobject
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
AddOutputFilterByType DEFLATE application/javascript application/json application/xml
AddOutputFilterByType DEFLATE image/svg+xml font/ttf application/vnd.ms-fontobject
</IfModule>
The IfModule wrappers make the rules safe: if a module is not installed, the block is skipped instead of causing a 500 error. If neither works, ask your host to enable mod_deflate or mod_brotli.
Enable Gzip compression: enable compression on Nginx
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
gzip_types text/plain text/css application/javascript application/json
application/xml image/svg+xml font/ttf;
# Requires the ngx_brotli module
brotli on;
brotli_comp_level 5;
brotli_types text/plain text/css application/javascript application/json
application/xml image/svg+xml font/ttf;
Text/html is always compressed by Nginx when gzip is on, so it does not need to be listed. Levels 4 to 6 give most of the saving without much CPU cost for on-the-fly compression.
Pre-compressing static files
For maximum Brotli compression (level 11) without slowing responses, build tools can create .br and .gz files at deploy time. Nginx then serves them with brotli_static on; and gzip_static on;. This is common for JavaScript-heavy sites built with Vite or webpack.
Compression with Cloudflare and CDNs
Cloudflare compresses responses to visitors with Brotli or Gzip automatically, regardless of what your origin server sends. Other CDNs such as BunnyCDN and the Hostinger CDN also support Brotli. See our Cloudflare setup guide and what a CDN does.
What not to compress
- JPG, PNG, WebP, AVIF and GIF images are already compressed; optimise them instead with our image compression guide.
- MP4 and other video files.
- WOFF2 fonts, which already use Brotli internally.
- ZIP and PDF downloads, which gain little.
Frequently asked questions
Is Brotli better than Gzip?
For text, yes: smaller files at similar speed for decompression. Keep Gzip enabled as a fallback.
Does compression use a lot of server CPU?
At moderate levels, very little. Caching plugins and CDNs also cache compressed copies so the work is not repeated.
Why does PageSpeed still say "Enable text compression"?
Usually a third-party script or a file type not in your list, such as JSON or SVG. Check the URLs it lists.
Related guides: Why Website Speed Matters and How to Make Your Site Faster, How to Get Your Website on Google and 301 vs 302 Redirects.
For official guidance, see web.dev by Google.
Need a faster site?
Our speed optimization service configures compression, caching and CDNs on your hosting. Contact us.
Written by the ItsWebExpert team, a web design and development company in Karachi building websites, online stores and apps for businesses across Pakistan.
Ask a Question on WhatsApp