How to Enable Gzip Compression in Apache and Nginx

October 21, 2025 / Servers, Hosting & Email

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.

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

  1. Check if mod_deflate is enabled
    1.  Apache uses the mod_deflate module for Gzip compression. To verify if it’s enabled, run:
      apachectl -M | grep deflate
    2. If there’s no output, enable it with:
      sudo a2enmod deflate
      sudo systemctl restart apache2
  2. Configure Gzip in Apache
    1. Open your main Apache configuration file or the .htaccess file of your website.
    2. Typical file locations include:
      /etc/apache2/apache2.conf
      /etc/httpd/conf/httpd.conf
      /var/www/html/.htaccess
    3. 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>
    4. Save the file and restart Apache:
      sudo systemctl restart apache2
  3. Verify Gzip Compression
    1. Check whether Gzip is enabled using the command:
      curl -H “Accept-Encoding: gzip” -I http://yourdomain.com
    2. If you see Content-Encoding: gzip in the response, Gzip is successfully enabled.
    3. Alternatively, you can test using an online tool like KeyCDN’s HTTP Header Check.

Enable Gzip Compression in Nginx

  1. 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”;
  2. 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
  3. 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

Spread the love