--> Enabling GZIP compression – Speed Up Your Site Help

New to site? Create an Account


Lost password? (close)

5 GZIP Compression

GZIP is a method of compressing files (also a file format) for faster network transfers. Enabling GZIP compression is a standard practice. If you are not using it, your webpages are likely slower than they should be.

GZIP performs best on text-based assets: CSS, JavaScript, HTML. All modern browsers support GZIP compression and will automatically request it. Your server needs to be configured to enable GZIP compression. Some CDNs require special care to ensure that GZIP is enabled. GZIP is a generic compressor that can be applied to any stream of bytes: under the hood it remembers some of the previously seen content and attempts to find and replace duplicate data fragments in an efficient way.

However, in practice, GZIP performs best on text-based content, often achieving compression rates of as high as 70-90% for larger files, whereas running GZIP on assets that are already compressed via alternative algorithms (e.g. most image formats) yields little to no improvement.

The three ways to enable compression by adding a bit of code via either a .htaccess file or an Apache/Nginx server configuration file. Let’s start with the .htaccess route.

Enable compression via .htaccess

The .htaccess file controls many important things for your site. If you are not familiar with the .htaccess file, please read my working with .htaccess article to get some know how before changing it. Following is the code that should put in the .htaccess file.

<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 handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

Save the .htaccess file and upload it to the server.

Compression on NGINX webservers

For NGINX add the following code to your config file

gzip on;
gzip_comp_level 2;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# Disable for IE < 6 because there are some known problemsgzip_disable “MSIE [1-6].(?!.*SV1)”;
# Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6gzip_vary on;

Compression on Apache webservers

The .htaccess technique also work on Apache If you see this .htaccess solution is not working then try below commands instead.

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript


You can check whether your compression is working or not using this tool: GZIP.