After we touched several aspects of caching let's return to gzip and review very simple and powerful technique 'Static gzip'.
What is Static Gzip?
Static Gzip is a way to serve compressed content w/o its actual compression 'on fly' (here is a blog post about gzip). Very roughly we have gzipped files and serve them instead of common ones. How can we do this?
General algorithm
- First of all we need to have gzipped versions of initial files. Usually they are named with a postfix
.gz
, i.e.main.css.gz
. As far as these files are static we can have them compressed at maximum. With Linux you can do the following
to get the smallest compressed file from the initial one.gzip -c -n -9 main.css > main.css.gz
- Secondly we must have any way to route HTTP requests to take compressed version of the file. Via Apache and
mod_rewrite
we can add the following rules to.htaccess
file or Apache configuration.<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Konqueror
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)\.css$ $1.css.gz [QSA,L]
<FilesMatch \.css\.gz$>
ForceType text/css
</FilesMatch>
</IfModule>
<IfModule mod_mime.c>
AddEncoding gzip .gz
</IfModule>
Rip off the veil of the mystery
What does this set of rules actually do?
- First of all we enable RewriteEngine (it may have been already enabled in your configuration).
- Then
rule selects all HTTP requests withRewriteCond %{HTTP:Accept-encoding} gzip
header and allows Apache to perform other rules.Accept-Encoding: gzip
- Then we skip Konqueror browser
because it seems it doesn't understand compressed content for CSS / JavaScript files.RewriteCond %{HTTP_USER_AGENT} !Konqueror
- Then we check if there is physical file with postfix
.gz
RewriteCond %{REQUEST_FILENAME}.gz -f
- And only after all these checks we perform actual internal rewrite rule.
We redirect all requests toRewriteRule ^(.*)\.css$ $1.css.gz [QSA,L]
.css
files to.css.gz
files. - After this redirect (which is the last one — modificator
[L]
) we forceContent-Type
of such files to betext/css
. Most servers send.gz
with default archive encoding, and browsers can't properly detect this content. - Finally with
AddEncoding gzip .gz
rule sets properContent-Encoding
header for compressed content.
Of course this logic can be applied not only to CSS files but to all files which can be efficiently compressed — JavaScript, fonts, favicon.ico, etc.
That's all?
Graceful Degradation
What if Apache doesn't have mod_rewrite
module? Or we need to serve CSS content via PHP? Or there is environment which doesn't support such logic (CGI / IIS)?
Well we can perform the whole algorithm actually via PHP too. All what we need here is the following.
- Form compressed file name (usually just with a postfix
.gz
). And check for this file's existence. - If no such file exists, create compressed version of the file (i.e. via
gzcompress
function). - Then write this compressed content to a new file (with already defined 'gzipped' file name).
- And set for this file time of change (mtime) to the value equals to initial (non-compressed) one. Why it's required? Because we can with check for existence also check if gzipped version of the current file has the same mtime and skip its re-creation in case of equivalence. If the current file and its compressed version have different time of change — it seems we need to re-create latter.
So with this logic we just check for file's existence and perform 2 checks for mtime (all such checks can be cached on file system level, or cache folder can be mapped to shared memory) and serve gzipped version. CPU is saved (with 80-85% of transferred content size)!
So Web Optimizer has all such approaches integrated and with version 0.6.7+ allows you to create .gz
versions of CSS / JS files within any folder on your website.
สล็อตออนไลน์ ยอดฮิตกับ PGSLOT เว็บน้องใหม่เแตกง่าย ปลอดภัย สะดวกเล่นได้ทุกที่ทุกเวลา รวยทางลัดได้ง่ายๆ สนุกจนลืมเวลาก็ PG SLOT ที่นี่ที่เดียว
ReplyDelete