How to Enable Compression for PageSpeed Insights

Google’s PageSpeed Insights says, “Compressing resources with gzip or deflate can reduce the number of bytes sent over the network.” Enabling gzip compression can significantly reduce the size of the transferred response. Best of all, it should clear the “Enable Compression” error in page speed tests.

Web Server Recommendations

There are different methods for enabling gzip compression depending on your web server. We’ll provide documentation for Apache, Nginx and IIS web servers. The most popular of these three is Apache.

For Apache

Copy and paste the following lines into your .htaccess file:


<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

After you save your .htaccess file, test your website again to make sure it’s working properly.

For Nginx

Please refer to the official documentation: ngx_http_gzip_module

For IIS

Please refer to the official documentation: Configure HTTP Compression