This is an easy to improve the speed of a webpage. The size of a page impacts the time it takes for the browser to download files. Enabling compression reduces that size by using gzip (similar to zipping a folder on your computer so you can email it to someone). How to enable this will depend on the type of server your website is hosted on.
For an Apache Server
For most people reading this, compression is enabled by adding some code to a file called .htaccess on their web host/server. This means going to the file manager (or wherever you go to add or upload files) on your webhost. If you are not familiar with the .htaccess file, please read Patrick Sexton’s working with .htaccess article to get some know how before changing it.
The code below should be added to your .htaccess file:
Save the .htaccess file and then refresh your webpage.
The instructions and code above will work on Apache. If they are not working there is another way that may work for you. If the above code did not seem to work, remove it from your .htaccess file and try this one 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
Check to see if your compression is working with these compression check tools:
- Enable GZIP Compression by Patrick Sexton
For an IIS (Microsoft) Server
<httpCompression> element specifies the HTTP compression settings for Internet Information Services (IIS) 7.
Note: An HTTP client must initiate communication for compressed content by sending the appropriate HTTP Accept-encoding header. If a client is not capable of HTTP compression, it will not pass that header and IIS 7 will always return uncompressed content.
IIS provides the following compression options:
- Static files only
- Dynamic application responses only
- Both static files and dynamic application responses
IIS 7 caches compressed static content in the path that is specified by the directory attribute, which increases compression performance by eliminating the need to recompress content that has already been compressed. After IIS 7 has compressed a file, subsequent requests are given the compressed copy of the file from the cache directory. You should use static compression with files that do not typically change, such as HTML files (*.html, *.htm), text files (*.txt), Microsoft Office documents (*.doc, *.xls, *.ppt), etc.
Note: Image files such as *.jpg and *.png files are also static files, but typically they do not benefit from HTTP compression because these image files are already compressed.
Unlike static compression, IIS 7 performs dynamic compression each time a client requests the content, but the compressed version is not cached to disk. Dynamic content is typically content that is created by an application and therefore changes often, such as Active Server Pages (ASP) or ASP.NET content. Since dynamic content should change often, IIS 7 does not cache it.
- HTTP Compression <httpCompression> by iis.net (this has an implementation example)
- Configure Compression (IIS 7) by Microsoft
- Configure HTTP Compression in IIS 7 by Microsoft
- Enable HTTP Compression of Dynamic Content (IIS 7) by Microsoft
- Enable HTTP Compression of Static Content (IIS 7) by Microsoft
For an NGINX Server
Compressing responses often significantly reduces the size of transmitted data. However, since compression happens at runtime it can also add considerable processing overhead which can negatively affect performance. NGINX performs compression before sending responses to clients, but does not “double compress” responses that are already compressed (for example, by a proxied server).
To enable compression, include the gzip directive with the on parameter.
By default, NGINX compresses responses only with MIME type text/html. To compress responses with other MIME types, include the gzip_types directive and list the additional types.
gzip_types text/plain application/xml;
To specify the minimum length of the response to compress, use the
gzip_min_length directive. The default is 20 bytes (here adjusted to 1000):
NGINX Compression via Varvy.com
To enable compression in NGINX you will need to add the following code to your config file:
- Compression and Decompression by NGINX
- Enable GZIP Compression on NGINX by EasyEngine
- Enable GZIP Compression by nginx.org
Leverage Browser Caching
Properly setting up browser caching can reduce the number of files a browser must download in order to render the page. For files that do not change often – like template css, js, and imagery – we recommend setting the browser cache to at least one month. Included in this audit is a list of resources that were flagged and their current cache length, evaluate these first.