Gzip compression is a commonly used technique to reduce the size of files sent from your web server to a user’s browser. It compresses text-based resources such as HTML, CSS, and JavaScript, improving page load speed and reducing bandwidth usage.
This article explains how to enable Gzip compression in both Apache and Nginx web servers.
Table of Contents
Importance of Enabling Gzip Compression
Enabling Gzip compression in Apache and Nginx is essential for improving website performance. It reduces file sizes, which helps pages load faster, consume less bandwidth, and improve user experience. Additionally, faster sites benefit SEO rankings and reduce server load and data transfer costs.
Enable Gzip Compression in Apache
- Check if mod_deflate is enabled
- Apache uses the mod_deflate module for Gzip compression. To verify if it’s enabled, run:
apachectl -M | grep deflate
- If there’s no output, enable it with:
sudo a2enmod deflate sudo systemctl restart apache2
- Apache uses the mod_deflate module for Gzip compression. To verify if it’s enabled, run:
- Configure Gzip in Apache
- Open your main Apache configuration file or the .htaccess file of your website.
- Typical file locations include:
/etc/apache2/apache2.conf /etc/httpd/conf/httpd.conf /var/www/html/.htaccess
- Add the following configuration block:
<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/json AddOutputFilterByType DEFLATE application/xml </IfModule>
- Save the file and restart Apache:
sudo systemctl restart apache2
- Verify Gzip Compression
Enable Gzip Compression in Nginx
- Edit the Nginx Configuration
Open your Nginx configuration file (usually located at /etc/nginx/nginx.conf) and add the following settings within the http block:gzip on; gzip_comp_level 5; gzip_min_length 256; gzip_vary on; gzip_proxied any; gzip_types text/plain text/css application/javascript application/json application/xml text/xml image/svg+xml; gzip_disable “msie6”;
- Test and Reload Nginx
Test the configuration for syntax errors:sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
- Verify Gzip Compression in Nginx
Run the following command to confirm Gzip is active:curl -H “Accept-Encoding: gzip” -I http://yourdomain.com
Look for the Content-Encoding: gzip header in the output. If it’s present, compression is working correctly.
Conclusion
This way, you can enable Gzip compression in Apache or Nginx. It is a simple yet powerful optimisation that significantly improves website performance.
By reducing file sizes before sending them to users, you can achieve faster page loading speeds, lower bandwidth consumption, and improved SEO performance.
Want to optimise your website performance further? Learn How to install NGINX on cPanel