Enabling Compression On Linux Servers

How did this come about? Well after trawling over the internet and finding nonsense that did not work I have managed to enable compression on our server. Within this blog short and sweet – I will advise you how to do so.

When wanting to get the most out of your business website, I have realised (I am keeping the English spelling within all texts ha ha) that keeping Google happy is a priority. If you click this link: https://developers.google.com/speed/pagespeed/insights/ you can enter your web address and sadly compression was an issue.

This help is for a WordPress site and may not be the same for other sites. It may put you in the right direction though in terms of what is needed.

If you go into the Http docs folder on your server (either using FTP https://filezilla-project.org/ or directly on your server PLESK panel) you will see a ht.access file. If you (if using FTP download and save, change and replace file) edit the ht.access file by adding the following code above the existing code:

<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>

There is also another way to enable compression (this is via ssh and http://www.putty.org/). This is done by running the following code through ssh directly by logging on to the Linux Server:

Login is an admin to your server and then run:

nano /etc/nginx/conf.d/gzip.conf

After this please add/create the file if it doesn’t exist. Enter the following:

gzip on;
gzip_disable “MSIE [1-6]\.(?!.*SV1)”;
gzip_proxied any;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;

To restart the nginx after the above change in command please run again via putty ssh on your server:

/sbin/service nginx restart

The way that we currently allow compression on our server (which Melbourne have said is more efficient is by adding) via the plesk control panel for your domain – within server settings and additional apache directives:

In additional HTTP directives add:

<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
<IfModule mod_headers.c>
# One week
<FilesMatch “\.(jpg|jpeg|png|gif|swf|js|css|svg)$”>
Header set Cache-Control “max-age=604800, public”
</FilesMatch>
</IfModule>

Additional HTTPS apache directives add:

<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
<IfModule mod_headers.c>
# Week
<FilesMatch “\.(jpg|jpeg|png|gif|swf|js|css|svg)$”>
Header set Cache-Control “max-age=604800, public”
</FilesMatch>
</IfModule>

Note the difference above is: The directive pertaining to client-side browser caching:

<IfModule mod_headers.c> # One week
<FilesMatch “\.(jpg|jpeg|png|gif|swf|js|css|svg)$”>
Header set Cache-Control “max-age=604800, public”
</FilesMatch>
</IfModule>

This will tell the browser to cache those files for one week (one to watch out for as this may cause unpredictable behaviour!)

Thanks to Jamie Ryland from www.fasthosts.co.uk and Julian Reilley at www.melbourne.co.uk whom has advised and supported my progress with their genius.