Most of the time, gzip compression will make your server perform better and more resource efficient. This tutorial will show you how to enable gzip compression for your nginx server.
Website gzip compression makes it possible to reduce the file size of a web file (like HTML, PHP, CSS and Javascript files) to about 30% or less of its original size before these files get sent to the browser. Offers you the power to enable and disable Gzip compression on your WordPress site. Supports Apache HTTP Server. If you need help or plugin is not working for you, consider the premium.
Gzip is a method of compressing files (making them smaller) for faster network transfers. It is also a file format but that’s out of the scope of this post.
Compression allows your web server to provide smaller file sizes which load faster for your website users.
Enabling gzip compression is a standard practice. If you are not using it for some reason, your webpages are likely slower than your competitors.
Enabling gzip also makes your website score better on Search Engines.
When a request is made by a browser for a page from your site, your webserver returns the smaller compressed file if the browser indicates that it understands the compression. All modern browsers understand and accept compressed files.
To enable compression in Apache, add the following code to your config file
To enable compression in Nginx, you will need to add the following code to your config file
As with most other directives, the directives that configure compression can be included in the http
context or in a server
or location
configuration block.
The overall configuration of gzip compression might look like this.
If you have a WordPress website and you can’t edit the Apache or Nginx config file, you can still enable gzip using a plugin like WP Super Cache by Automattic.
After installing the plugin, go to its Advanced Settings tab and check on the settings “Compress pages so they’re served more quickly to visitors. (Recommended)“ to enable gzip compression.
However, keep in mind that this plugin comes with a lot of more features, some of which you may not want. So if you don’t like the extra features, you can just use a more simple plugin like Gzip Ninja Speed Compression or Check and Enable Gzip Compression.
If you are using Firefox, do the following steps:
If you are using Chrome, do the following:
As you can see in the Firefox Developer Tools Network Tab, the compressed size is normally one third or one fourth the original size. This ratio differs from requests to requests but usually that’s the ratio for html, css, javascript and text files.
OK, that’s a smart question. Since the server has to do the extra work to compress the response, it may need some more CPU power. However, the CPU power that is saved during transferring the response usually makes up for that, not to say that more CPU power is saved. Therefore, at the end of the day, normally your server would be more CPU efficient.
Image files are usually already compressed, so gzip compressing the image will not save you a lot of bytes (normally less than 5%), but on the other hand requires a lot of processing resource. Therefore, you shouldn’t enable gzip for your images and should only enable gzip for html, css, javascript and text files.
The vast majority of websites today use some kind ofcompression, delivering their content, as most modern browserssupport compression. Compression is useful when applied to text(html, javascript, js), as images (usually png) are alreadycompressed. The result of compression is remarkable bandwidthsaving, so the site becomes more responsive. This is also a SEOadvantage.
Compression will slightly increase CPU usage, but in most casesthis is not a problem, comparing to compression advantages.
The most common compression solution is gzip compression. Almost allweb servers support gzip compression. Nginx has built-in httpcompression. This is probably a reason why Nginx rapidly gainspopularity.
Here I describe how to setup and test gzip compression in aDebian system with Apache using Apache module mod_deflate. Mod_deflate is the Apache2 successor ofApache v1.* mod_gzip. For other popular web servers you can findinformation here: IIS, Nginx.
In latest Debian version (Debian 6 or Squeeze),mod_deflate is installed and enabled. If not:
Edit website configutation file, for example:
add the following lines:
Explanation of above Apache directives:
That’s all. Don’t forget to restart Apache.
As an alternative, you can use an .htaccess file inyour webroot directory (with the same Apache directives).
If your site is PHP based, there is an alternative (notrecommended) method to apply gzip compression. Just makethe following changes to php.ini, save and restartApache:
In order mod_deflate to keep logs, add the followinglines to website configuration file (and restart Apache).
To check mod_deflate log file:
Example
Comments on above log file entries:
You probably need to remove logging in a production server,after you check compression results.
Suggested tools:
Gzip compression can be detected from http headers, looking forContent-Encoding: gzip. In this example, the pagehttp://www.pontikis.net/blog/ is examined:
Go to http://www.pontikis.net/blog/, press CTRL+SHIFT+I (orMenu button → Tools → Developer tools) and go to Network tab
Go to http://www.pontikis.net/blog/, press F12 to activateFirebug and go to Net tab
Using the online tool Redbot.org
We appreciate your feedback and suggestions. Leave us a commentbelow.
Entrepreneur Full-stack developer Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.