Enable GZIP Compression

In this Article

How GZIP compression works

GZIP compression enables HTML, CSS and JS content to be compressed so that the files sent from the server to the visitor's browser are smaller. A smaller filesize means a faster download which improves the overall loading time of your page. We have an in-depth explanation on our blog.

NOTE: GZIP compression can only be applied to files served from your own domain NOT to those from 3rd parties - Facebook, Google, etc

How WP Rocket applies GZIP

On Apache servers, WP Rocket automatically adds GZIP compression rules in your .htaccess file using the mod_deflate module. These are the rules we add to your htaccess file: 

# Gzip compression
<IfModule mod_deflate.c>
# Active compression
SetOutputFilter DEFLATE
# Force deflate for mangled headers
<IfModule mod_setenvif.c>
<IfModule mod_headers.c>
SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
# Don’t compress images and other uncompressible content
SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|mp3|avi|swf|mp?g|mp4|webm|webp|pdf)$ no-gzip dont-vary
</IfModule>
</IfModule>
# Compress all output labeled with one of the following MIME-types
<IfModule mod_filter.c>
AddOutputFilterByType DEFLATE application/atom+xml \
		                          application/javascript \
		                          application/json \
		                          application/rss+xml \
		                          application/vnd.ms-fontobject \
		                          application/x-font-ttf \
		                          application/xhtml+xml \
		                          application/xml \
		                          font/opentype \
		                          image/svg+xml \
		                          image/x-icon \
		                          text/css \
		                          text/html \
		                          text/plain \
		                          text/x-component \
		                          text/xml
</IfModule>
<IfModule mod_headers.c>
Header append Vary: Accept-Encoding
</IfModule>
</IfModule>
<IfModule mod_mime.c>
AddType text/html .html_gzip
AddEncoding gzip .html_gzip
</IfModule>
<IfModule mod_setenvif.c>
SetEnvIfNoCase Request_URI \.html_gzip$ no-gzip
</IfModule>

Therefore there are 2 requirements for GZIP compression to work:

  1. mod_deflate  has to be enabled on the server (ask your webhost), 
  2. The necessary rules placed in your server configuration file.htaccess file, located in the root directory of your site.

CSS/JS/HTML are pre-compressed

When WP Rocket creates the cache file for each page, it creates 2 versions - an HTML file and a GZIP file. The GZIP-ed version will usually be served, but the HTML is there as a fallback.

When File Optimizations are applied, such as Minify/Combine CSS and JS, WP Rocket also creates 2 versions of each file, but they are not served automatically.
Please see this guide for more information about this feature.

Your server must have the gzencode function available in PHP.

Troubleshooting "enable compression" warnings

  • "Compress components with GZIP" - Pingdom 
  • "enable text compression” - PageSpeed / Lighthouse
  • "enable compression" - GT Metrix
  • "compress components" - Yslow
  1. Pingdom does not provide the list of specific files that are being flagged, so if that’s what you used, re-run the test using either GT Metrix or PageSpeed.
  2. Then click on the message to expand it and view the list of files that are being flagged.

  3. Are the files listed hosted on your domain?
  4. If the files are external, i.e. not served from your own domain, you can't optimize them.

    You can read more about this in our doc: 
    Handling External Resources

    If the files referenced are hosted on your domain, check the following: 

  5. Does your site use an htaccess file? 
  6. As described above, WP Rocket adds GZIP  rules in the htaccess file.  Check with your host to see if:
    • Your site uses an htaccess file. If it does, check the file to make sure the block of rules above are present
    • Mod_deflate is enabled on the server
  7. What is the file size?
    Some servers, have default maximum and minimum file sizes for compression. If the file is very large or very small it may not be compressed.
  8. Not all components should be compressed
    For example, very small files will not benefit from compression, and some file types should not be compressed. More info

Server admins

If you are running your own server, here is some guidance about what you need in order to apply GZIP compression. Note we cannot provide support for modifications to your server config. We're providing this information as a helpful resource :)

Apache

If you run a dedicated Apache server you can activate mod_deflate by executing this command line:

a2enmod deflate

…and restart the server afterwards:

service apache2 restart

NOTE: In some rare cases, GZIP compression might not be applied only to HTML documents, even, if mod_deflate is enabled and .htaccess rules are present. You should then add the following directive to your user.ini or php.ini file placed in the WordPress root:
zlib.output_compression = On

Don't forget to check your site afterwards!

NGINX

Nginx doesn’t use an .htaccess. To enable GZIP on NGINX you can follow this guide:
easyengine.io/tutorials/nginx/enable-gzip/

Or you can use this alternate configuration for NGINX provided by one of our customers:
github.com/maximejobin/rocket-nginx

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.