Server-side compression: Gzip vs Brotli
Compression reduces the size of resources transferred between the server and the browser. Google recommends enabling it to improve
performance on mobile devices.
Gzip on Apache
Add this to the
.htaccess file or to the virtual host:
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
AddOutputFilterByType DEFLATE application/javascript text/javascript
AddOutputFilterByType DEFLATE font/opentype font/truetype font/eot
Brotli on Nginx
Requires compiling Nginx with the
ngx_brotli module. Recommended configuration:
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/json application/javascript;
Brotli achieves
20-26% better compression than Gzip with HTML/CSS.
Advanced cache configuration
Expiration headers on Apache
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
Caching on Nginx
location ~* .(js|css|png)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
}| Directive | Apache | Nginx |
|---|
| Compression | mod_deflate | gzip/brotli |
| Cache time | mod_expires | expires |
TTFB optimization
Reducing Time To First Byte involves:
- Enabling OPcache for PHP (cPanel: WHM > PHP Selector)
- Configuring keepalive_timeout in Nginx (15-30 seconds)
- Using mod_cache on Apache
Frequently asked questions
Gzip or Brotli for my server?
Brotli offers better compression but requires more CPU. Use Gzip if the server has limited resources.
How do I verify that compression is working?
Use Chrome DevTools > Network and check the
Size/Content column, or tools like
Gzip Test.
What file types should I avoid compressing?
Binary images (JPEG, PNG) and files that are already compressed (ZIP, PDF). Compression would be redundant.
How does caching affect TTFB?
Caching reduces server load by avoiding the need to reprocess identical requests, improving TTFB by up to 40% according to
NGINX.