Drupal: Traffic Compression
This article tells how to speed-up CSS and JS files uploading by means of compression on server and passing compressed copy to browser.
Solutions
Automated compression by server
You should insert the following code to .htaccess file:
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_item_include file \.js$
mod_gzip_item_include file \.css$
</IfModule>
Create archive for every JS/CSS file and send it nstead of unpacked files manually
You should archive all these files manually and upload them to the server. It is a fag but number of files cannot approach infinity… And there is no server loading for creation packed file copy.
Add *.js files to *.js.gz archive and upload to server to the same folder where original JS file is located.
Insert the following code to .htaccess file (in the site root) right after RewriteEngine on
:
RewriteRule ^(.*\.js\.gz)$ – [L]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz
Checking if it works correctly
Load a page in browser and view request headers:
- Response Headers
- Date Wed, 19 Mar 2008 12:32:09 GMT
- Server Apache
- Cache-Control max-age=1209600
- Expires Wed, 02 Apr 2008 12:32:09 GMT
- Last-Modified Wed, 19 Mar 2008 12:31:38 GMT
- Etag "bdcf97-87d-47e107aa"
- Accept-Ranges bytes
- Content-Length 2173
- Connection close
- Content-Type application/x-gzip
- Content-Encoding gzip
Size of the received file should be smaller…
Operational principle
mod_rewrite decides what file (compressed or no) is to be sent to the client:
Operational principle:
If compressed copy javascript.js.gz is be located near javascript.js file and request contains a note that client maintains gzip-encoding then client gets compressed copy. If compression isn’t maintained or there is no compressed file, client gets original file as a response.
As a result, server traffic and server loading are decreased. The same thing can be applied for CSS.
Using mod_deflate
There is a suggestion to deliver mod_deflate in standard apache 2.x
Using nginx web-server for traffic compression
You can also use nginx. It can compress a folder immediately or perform your request: send compressed files instead of originals.