Gzip is the most popular compression algorithm used to compress web files and reduce their size. A compression algorithm is a way of making files (ie. HTML, CSS, JavaScript, and other web assets) smaller so they take up less space. Using Gzip compression on your website can help to speed up page load times for your visitors because smaller files can be downloaded faster. On average, Gzip can achieve a compression rate of around 60-80% for text-based files, such as HTML and CSS.
How does Gzip work? In short: Gzip compresses a file before it's sent to the browser of the user (ie. a visitor on your website) and their browser then decompresses the Gzipped file automatically to load the full original file in their browser.
Most modern web servers support Gzip compression, and it is often enabled by default. To check whether Gzip is enabled on your website you can use our Gzip compression test or our website speed test.
You can actually use two different Apache mods to enable HTTP Gzip compression: mod_gzip and mod_deflate. Mod_gzip enables Gzip compression and mod_deflate makes it possible to compress the output from your server before it is being sent to your visitor (which is the same thing). So should you be compressing your resources with Gzip or deflate? In the end it doesn't matter much, both modules will provide you with the same maximum Gzip compression possible. But, as a general rule it is recommended to use mod_deflate since it’s more widely supported compared to mod_gzip. Mod_deflate is also better documented and easier to configure. If mod_deflate doesn't work on your server you can still use mod_gzip. Not every host has these modules enabled on their servers, so make sure you ask your host about this when the below .htaccess scripts do not work. Add one of the below scripts to your .htaccess file (which can be found or should be placed in the root folder of your website (usually /var/www/html/)):
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/opentype
# For Olders Browsers Which Can't Handle Compression
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
You can download the above script in a .htaccess file here.
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_include handler ^cgi-script$
</ifModule>
Download the above script in a .htaccess file here.
When you just want to compress certain components or a certain file type with Gzip, make sure that you include this file type to one of the above lists. And again, make sure at least one of the mods is enabled on the server. If you don't want to compress a certain file type, just remove this file type from the code.
Please follow the below steps to enable Gzip in Nginx:
Open the Nginx configuration file for your website using your preferred text editor. The configuration file is usually located in /etc/nginx/nginx.conf or /etc/nginx/sites-available/your-site.conf, depending on your setup.
gzip on;
gzip_types text/plain text/xml text/css application/json application/javascript application/xml application/xml+rss text/javascript image/svg+xml;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
sudo service nginx restart
That's it! Now test your website using our gzip test to see if what you changed is working!
When your website is hosted on a Windows server, you can enable compression for both static and dynamic files. It’s fairly easy to set this up in the IIS manager:
When you can't get it working visit this page to learn how else you can set it up in IIS 10.
There are multiple plugins available to enable gzip compression in WordPress. They are easy to find in the "Add a plugin" section of the WordPress admin, some work better than others. However, please note that it is recommended to always use one of the above server side methods to enable Gzip.
The short answer: Yes, but with a note. What is Brotli? Brotli is a compression algorithm developed by Google in 2013. On average, Brotli is able to achieve 20-30% better compression rates than Gzip. In practical use cases, Brotli can achieve compression rates of up to 90% for text-based files (ie. HTML, CSS, and JavaScript). Brotli also has faster decompression speeds than Gzip.
The note: Brotli was initially developed for use with the Google Chrome web browser, but has since been adopted by pretty much all other web browsers and web servers. Unfortunately, not all yet. This is why, at this moment, it is still better to use Gzip as the main compression method for all browsers. You can however, use a "switch strategy" where you can switch between Gzip and Brotli depending on which browser the user is using. More on this very soon!
We can help with that! We have professionally optimized the speed of thousands of websites. Because of this we know how to get the best results quickly while keeping our prices low. Get an instant price quote for our services now.