Enable GZIP compression
This article contains information about the GZIP compression, its definition, how WP Rocket applies it, troubleshooting and guidance for server administrators.
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/LiteSpeed 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:
- mod_deflate has to be enabled on the server (ask your webhost),
- 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
- 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.
- Then click on the message to expand it and view the list of files that are being flagged.
- Are the files listed hosted on your domain? If the files are external, i.e. not served from your own domain, you can't optimize them.
- Does your site use an htaccess file? 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
- 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. - 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
You can read more about this in our doc:
Handling External Resources
If the files referenced are hosted on your domain, check the following:
HTML not GZIP-ed
In some specific server configurations, all the static assets will be correctly compressed, but you may receive a warning specifically about the HTML document not being compressed.
If this is the case, try enabling ZLIB compression on your server by adding the following directive to your user.ini or php.ini file placed in the WordPress root:
zlib.output_compression = On
If this doesn't work, you may need to contact your host so they can help you get GZIP compression enabled.
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/LiteSpeed
If you run a dedicated Apache/LiteSpeed server you can activate mod_deflate by executing this command line:
a2enmod deflate
And restart the server afterwards:
service apache2 restart
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
IIS - Windows
IIS servers don't use an htaccess file. GZIP has to be enabled on the server configuration level. You can follow this guide to enable GZIP on IIS servers:
docs.microsoft.com/en-us/iis/extensions/iis-compression/using-iis-compression