How to enable Gzip compression

Last updated: Feb 21, 20234 Min read

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.

Enable Gzip compression in Apache using the .htaccess file

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/)):

To enable mod_deflate (recommended):

<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.

To enable mod_gzip (second back up choice):

<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.

A note:

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.

Enable Gzip compression in Nginx

Please follow the below steps to enable Gzip in Nginx:

Step 1: Edit the Nginx Configuration File

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.

Step 2: Add the following code to the http block:

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;

Here's what each line does:

  • gzip on; turns on Gzip compression.
  • gzip_types specifies the types of files that should be compressed. You can add or remove file types as needed.
  • gzip_vary on; adds a Vary: Accept-Encoding header to the response, which tells caching servers to store multiple copies of the response for different compression types.
  • gzip_comp_level sets the compression level. The value can range from 1 to 9, with 1 being the fastest and least compressed, and 9 being the slowest and most compressed. A value of 6 is a good balance between speed and compression.
  • gzip_buffers sets the number and size of memory buffers used for compression.
  • gzip_http_version specifies the minimum version of HTTP required to use Gzip compression.

Step 3: Restart Server & Test

After making the changes to the configuration file, save and close it, and then restart Nginx using the following command: sudo service nginx restart

That's it! Now test your website using our gzip test to see if what you changed is working!

Gzip on Windows Servers (IIS Manager)

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:

  • Open up the Windows Feat
  • Click on the site you want to enable compression for
  • Click on "Compression" (under IIS)
  • Now make sure that the "Enable dynamic content compression" and "Enable static content compression" boxes are ticked and you are done!

When you can't get it working visit this page to learn how else you can set it up in IIS 10.

Enable Gzip in WordPress

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.

Brotli - A better alternative to 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!

Want To Have The Maximum Page Speed Possible For Your Website?

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.