You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Christian Folini <ch...@post.ch> on 2007/10/31 09:42:38 UTC

[users@httpd] poor man's mod_deflate (mod_rewrite recipe)

Hi there,

We have been playing around with mod_deflate and caching
the other day to solve an urgent performance problem, where
mod_deflate was not an option.

We got some sort of poor man's mod_deflate out of it.

It works on static files only and it means you have to
gzip the static files in the filesystem in order to make
it work. But afterwards you'll get a really fast server.

I think it is worth posting it here, as it is a good example for
mod_rewrite use.

<VirtualHost *:80>
    RewriteEngine On

    RewriteCond "%{HTTP:Accept-Encoding}" "gzip.*deflate|deflate.*gzip" 
    RewriteCond /data/custom-apaches/apache-2.0.61/htdocs/%{REQUEST_FILENAME}.gz -f
    RewriteRule /(.*) /$1.gz [last,env=deflated:1]

    Header set Vary             "Accept-Encoding" env=deflated
    Header set Content-Encoding "gzip"            env=deflated

</Virtualhost>

Adjust your paths accordingly.

Explanation:
On the first RewriteCond line, we check wether the browser accepts
deflated content (Accept-Encoding Header).
On the next line, we check wether there is a gzip file to the non-zipped
filename, that the user requested. If there is one, then the RewriteRule
comes into play. Here we rewrite the filename to filename.gz and set
the environment variable "deflated" to 1.

The effect of this construct is, that unless the browser asks for
zipped content and unless there is a zipped file in the filesystem,
the server will behave as usual. However, if these conditions are met, it
sends the zipped file instead of the standard one.

During the response phase, we add two headers to the response if the
environment variable "deflated" is set.

You may have to tweak this to work on full paths and not only on
filenames. You may also want to write a cronjob to zip your files
from time to time. And maybe you just go for the real thing and
install mod_deflate. Otherwise, I enjoyed writing this hack.

regards,

Christian


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] poor man's mod_deflate (mod_rewrite recipe)

Posted by Christian Folini <ch...@post.ch>.
On Wed, Oct 31, 2007 at 12:39:40PM +0000, Nick Kew wrote:
> > I think it is worth posting it here, as it is a good example for
> > mod_rewrite use.
> 
> It's a very BAD example of mod_rewrite use, because you're reinventing
> a wheel that's been implemented for over 10 years in Apache.
> See mod_negotiation.

Hey Nick,

Glad I posted it nevertheless. That way I had you put my nose down
on the mod_negotiation documentation - which I considered too
complicated so far.

When facing a problem, I tend to use those tools which I understand.
That may be reinventing the wheel, but it feels better then
playing around with a productive server in a hurry.

And now I'll turn back to your Apache Modules book that
landed on my desk today. Expecting an interesting day.

regs,

Christian


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] poor man's mod_deflate (mod_rewrite recipe)

Posted by Nick Kew <ni...@webthing.com>.
On Wed, 31 Oct 2007 09:42:38 +0100
Christian Folini <ch...@post.ch> wrote:

> It works on static files only and it means you have to
> gzip the static files in the filesystem in order to make
> it work. But afterwards you'll get a really fast server.

That's perfectly normal.

> I think it is worth posting it here, as it is a good example for
> mod_rewrite use.

It's a very BAD example of mod_rewrite use, because you're reinventing
a wheel that's been implemented for over 10 years in Apache.
See mod_negotiation.

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org