Friday, 22 November 2013

Compressing web pages and Optimize website speed with mod_expire and mod_deflate



The mod_deflate module allows the Apache2 web service to compress files and deliver them to clients (browsers) that can handle them. With mod_deflate you can compress HTML, text or XML files by up to 70% of their original sizes. Thus, saving you server traffic and speeding up page loads.

NOTE:
  • Compressing files will increase load on your server, but it is a small tradeoff considering your client's connection times will decrease significantly.
  • This will not exclude users with older browsers that cannot handle compressed content. The browser negotiates with the server before any file is transferred, and if the browser does not have the capability to handle compressed content, the server delivers the files uncompressed.
  • mod_deflate has replaced Apache 1.3's mod_gzip in Apache2.
  • This article shows how to enable mod_deflate globally across all the domains on your (dv) server. Should you only wish to enable for a single domain you'd need to add the AddOutputFilterByType and BrowserMatch rules below to the VirtualHost section in your configuration.
Now the question is how to speed up your Apache webserver with mod_deflate and mod_expire.
mod_expire controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.
Like mod_gzip, mod_deflate allows you to compress files before they are served to the client, saving your server traffic.
Compressing causes a increased load on the server but the clients connections times decrease a lot, it might decrease the response times with 25% to 50%. The Apache package shipped with CentOS 5 is not compiled with mod_gzip but mod_deflate instead. With mod_deflate you can compress HTML, XML, CSS, text, JavaScript, ect.

mod_deflate

By default mod_deflate is enabled on CentOS 5. To be sure open the file /etc/httpd/conf/httpd.conf and look for the line showing:
LoadModule deflate_module modules/mod_deflate.so
and make sure it is not comment out.
Next create a new file /etc/httpd/conf.d/deflate.conf with the lines:

<IfModule mod_deflate.c>
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/xml
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/x-javascript
 
 DeflateCompressionLevel 9
 
# Browser specific settings
 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4\.0[678] no-gzip
 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 BrowserMatch \bOpera !no-gzip 

# Setup custom deflate log
 DeflateFilterNote Input instream
 DeflateFilterNote Output outstream
 DeflateFilterNote Ratio ratio
 
 LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
# Example of log file
 CustomLog logs/deflate_log DEFLATE
</IfModule>

mod_expire

Also by default mod_expire is enabled on CentOS 5. To be sure open the file /etc/httpd/conf/httpd.conf and look for the line showing
LoadModule expire_module modules/mod_expire.so
and make sure it is not comment out.
Now create a new file /etc/httpd/conf.d/expire.conf with the lines:
<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresByType image/jpg "access plus 60 days"
 ExpiresByType image/png "access plus 60 days"
 ExpiresByType image/gif "access plus 60 days"
 ExpiresByType image/jpeg "access plus 60 days"
 ExpiresByType text/css "access plus 1 days"
 ExpiresByType image/x-icon "access plus 1 month"
 ExpiresByType application/pdf "access plus 1 month"
 ExpiresByType audio/x-wav "access plus 1 month"
 ExpiresByType audio/mpeg "access plus 1 month"
 ExpiresByType video/mpeg "access plus 1 month"
 ExpiresByType video/mp4 "access plus 1 month"
 ExpiresByType video/quicktime "access plus 1 month"
 ExpiresByType video/x-ms-wmv "access plus 1 month"
 ExpiresByType application/x-shockwave-flash "access 1 month"
 ExpiresByType text/javascript "access plus 1 week"
 ExpiresByType application/x-javascript "access plus 1 week"
 ExpiresByType application/javascript "access plus 1 week"
</IfModule>


Share:

No comments:

Post a Comment