Forum Moderators: phranque

Message Too Old, No Replies

How can I pre-compress static content in httpd2.x

         

haha

11:16 am on Dec 13, 2007 (gmt 0)

10+ Year Member



I'm using httpd2.0.x with mod_deflate enabled
but the cpu load is too high
so i want to pre-compress static contents
How can I do that pls

jdMorgan

4:29 pm on Dec 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you could use gzip to compress the files and save them as <original-filename>.gz
Then examine incoming "HTTP accept encoding" client request headers. If a client indicates that it can accept gzip encoding, and a .gz version of the requested file exists, then internally rewrite the request from <original-filename> to <original-filename>.gz and serve that file.

That's just one way to do it. You can get fancy and do the first step using a cron job, or add another function to invoke gzip if a gzipped version of the file does not exist or if it is older than the If-Modified-Since request header sent by the client.

This is just one of many ways to do it -- There are many possibilities.

Jim

haha

2:00 am on Dec 14, 2007 (gmt 0)

10+ Year Member



I have used it like this:

RewriteEngine on
RewriteLog "logs/modrewrite.log"
RewriteLogLevel 9
# If client accepts compressed files
RewriteCond %{HTTP:Accept-Encoding} gzip
# and if compressed file exists
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.gz -f
# send .html.gz instead of .html
RewriteRule ^(.+)\.(js&#166;xml&#166;css&#166;html)$ $1.$2.gz [L]

the file abcd.xml can be rewrite to abcd.xml.gz£¬but it is not used yet
while I access http://www.example.com/abcd.xml using firefox,
it popup a dialog to ask me save or open a file?!

I checked the response from the server(httpd2.x)

HTTP/1.1 200 OK
Date: Fri, 14 Dec 2007 01:57:25 GMT
Server: Apache/2.2.2 (Fedora)
Vary: Accept-Encoding
Last-Modified: Thu, 13 Dec 2007 09:41:42 GMT
ETag: "4d08b0-fe-c0dbc180"
Accept-Ranges: bytes
Content-Length: 254
Cache-Control: max-age=1151056
Expires: Thu, 27 Dec 2007 09:41:42 GMT
Content-Type: application/x-gzip

I think maybe it missed a HTTP header "Content-Encoding: gzip"?

[edited by: jdMorgan at 5:17 am (utc) on Jan. 9, 2008]
[edit reason] example.com [/edit]

jdMorgan

3:39 am on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, looking at a few previous threads here, try leaving the Content-type as it was (e.g. text/html) and indicate that it's gzip'ed using Content-Encoding.

Jim

haha

3:58 am on Dec 14, 2007 (gmt 0)

10+ Year Member



?
pls say in details
or give me a url
:)

How can I insert http header "Content-Encoding: gzip" into server's response?by using mod_header?

thx very much

haha

10:58 am on Dec 14, 2007 (gmt 0)

10+ Year Member



I have solved the problem bye using mod_rewrite + mod_header

RewriteEngine on
RewriteLog "logs/modrewrite.log"
RewriteLogLevel 9
# If client accepts compressed files
RewriteCond %{HTTP:Accept-Encoding} gzip
# and if compressed file exists
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.gz -f
# send .html.gz instead of .html
RewriteRule ^(.+)\.(html¦xml¦css)$ $1.$2.gz [T=text/$2,E=GZIP:gzip,L]
Header set Content-Encoding gzip env=GZIP

jdMorgan

1:34 pm on Dec 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks good. However, I was thinking something like:

<FilesMatch "\.gz$">
Header set Content-Encoding gzip
</FilesMatch>

This <FilesMatch> method also works with Apache1.3, unlike the conditional Header directives which are supported only on Apache2.0 and above.

Jim

haha

4:02 am on Jan 9, 2008 (gmt 0)

10+ Year Member



RewriteRule ^(.+)\.(html&#166;xml&#166;css)$ $1.$2.gz [T=text/$2,E=GZIP:gzip,L]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<snip>
This line have a mistake:
The mime header will be replaced with "text/$2" when the rewriterule take
effect!
How can I fix it?

[edited by: jdMorgan at 5:16 am (utc) on Jan. 9, 2008]
[edit reason] tidying-up [/edit]

haha

4:51 am on Jan 9, 2008 (gmt 0)

10+ Year Member




RewriteRule ^(.+)\.(html&#166;xml&#166;css)$ $1.$2.gz [T=text/$2,E=GZIP:gzip,L]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<snip>
It is fine using $2 in the flag fieldin httpd 2.2.x, but not in the
httpd 2.0.x!

[edited by: jdMorgan at 5:17 am (utc) on Jan. 9, 2008]
[edit reason] Tidying-up. [/edit]

jdMorgan

5:15 am on Jan 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, I commend the <FilesMatch> method described above to you as a more-portable solution.

Jim