Enable Compression

DifficultyEasy
PriorityHigh
Average Score83%

Brief

Enabling compression for web resources can significantly improve loading times as the page load data can be reduced by up to 90%.


Description

Compressing web resources dramatically reduces the download data for the user, which in turn reduces loading time for the user, resulting in a much better user experience. Deploying GZIP compression for all your compressible web resources is always recommended as GZIP is ubiquitously supported by the vast majority of browsers.


How to enable GZIP compression?

Majority of the default installations of the webservers have Gzip compression enabled. There are a couple of ways you can start using the advantage of GZIP compression on your webserver. Most hosting companies uses Apache, but some uses ngnix for faster performance. Get in touch with your hosting provider to see what webserver does they use. These are the few ways you can enable GZIP compression in Apache, Ngnix and IIS.

How to enable GZIP compression in Apache:

There are two ways we can enable GZIP compression in Apache, by mod_deflate or by mod_gzip. The best to way make use of GZIP compression in Apache is using mod_defalte.It is possible simply by editing .htaccess file on your website root folder and adding the following piece of code. On a live website, one can use any FTP clients to find the .htaccess file. Add these codes on the .htaccess file.


<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 MSIE !no-gzip !gzip-only-text/html
</IfModule>

Make sure to add these contents below the current .htaccess rules.

How to enable GZIP compression in NGNIX:

To enable GZIP compression in ngnix one has to directly edit the ngnix config file. Open ngnix.conf. By default it should be located at /etc/nginx/nginx.conf Locate the gzip section.

##
# gzip Settings
#
#
gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

You could see gzip is enabled by default, but the rules are disabled by commenting it out using #. To enable the rules and make the gzip work, remove the # before each line - it should look like this.


                                        ##
# gzip Settings
#
#
gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

                                        

Save the file and restart ngnix.

How to enable GZIP compression in IIS:

IIS supports two types of compression, Static and Dynamic. Static compression is to compress the normal static file which resides in the hard disk. IIS caches the static compressed files for better performance. Dynamic compression will compress the dynamic request from various modules and not cached by IIS, because these data changes very often.

Enable compression in IIS

Open IIS >> Select the server name>> Click on “compression”:


Here, you can enable static or dynamic compression for the server as per your needs



To enable this feature for a single domain: Open IIS >> navigate to the desired domain >> Click on “Compression” and tweak accordingly.



Enable Compression on applicationHost.config (Server Level) or web.config (per-Domain Level)

To enable compression in the server level, add the below code in applicationHost.config file of IIS. It is possible to use the same code in the web.config to enable compression for a domain.

<system.webServer>
 <httpCompression directory="%SystemDrive%inetpub	empIIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%system32inetsrvgzip.dll" staticCompressionLevel="9" />
    <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />
    </system.webServer >