Forum Moderators: phranque

Message Too Old, No Replies

gzip

         

helenp

7:38 am on Oct 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi,
This I posted in webmaster general, but having a second thought, I think the wrigt place is here.

My shared host does not have gzip enabled,
so I read how to use gzip despite of that.

I have minified my 3 .js files (about 200 kbs), I have made them 1 file then I gzipped them with an online tool, so I now have a gzipped file with 36kb.
I renamed the .gz to .jgz (for safari).

I have this on top of my testpage:
<?php
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else
ob_start();
?>
in the html I have the .js as a link
and in the htaccess I have this:
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.js$ $1\.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz
<FilesMatch "\.(gif|jpeg|jgz|gz|jpg|js|pdf|css|GIF|png|flv|swf|ico)$">
Header set Cache-Control: "max-age=1296000"
</FilesMatch>

I can see the jgz file is being cached using firebut/page speed.
To start with I saw the cpu going up much, maybe because I still hadnīt added .jgz to cache-control, now I dont see it goes up much when I reload the testpage I done.
So I wonder if it takes much cpu to only unzip (decompress) the gzipped file, and if the decompression are cached and if not if it is possible to cache.
If it dont take much cpu I would gzip more thinks but dont dare until I have added this code to all pages, at the moment only testing, afraid the cpu will raise.

Also wonder about Zlid.
I read in php site that zlib is better than Ob_gzhandler.
I dont know how to use it or if its enabled a not, in a way it looks enabled and in another turned off.
This is from my phpinfo:
ZLib Support enabled
Stream Wrapper support compress.zlib://
Stream Filter support zlib.inflate, zlib.deflate
Compiled Version 1.2.3
Linked Version 1.2.3

DirectiveLocal ValueMaster Value
zlib.output_compressionOffOff
zlib.output_compression_level-1-1
zlib.output_handlerno valueno value

Would appreciate advice.
Thanks in advance.
Helen

httpwebwitch

5:28 am on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My shared host does not have gzip enabled


wouldn't it be easier to call your host, and ask them to enable gzip?

the way you're doing it - all the buffering and whatnot - will consume memory & CPU resources like stink. whereas the gzip module for Apache (mod_deflate) is incredibly well optimized and EASY to turn on.

You asked for advice, I'm saying throw away all this rewriting and ob_start stuff and do it right.

lucy24

6:19 am on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If your headers contain the line

HTTP_ACCEPT_ENCODING=gzip,deflate

does that mean you're good to go and everything will magically happen without any more action on your part?

(I just looked up mine. Never knew there was a mod_deflate!)

helenp

6:54 am on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, thanks for your answer, I thought that doing it on the fly takes even more cpu as the server has to compress them also. Thought a pre-gzipped file would take less cpu.

Doing a search I found this thread using the rewrite in htaccess written by jdMorgan, however not the ob_start:
[webmasterworld.com...]

Is the ob_start what is taking a lot of cpu?

I will ask, however I doubt they will do as it is a shared hosting.

helenp

10:02 am on Oct 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now I am really confused.
I just did a test.
1. Having the rewriterule etc in .htaccess with the ob_start("ob_gzhandler"); at beginning of page gives me a document size of 386 kb (big page with photos and the big .js file)
2. I took away the ob_start("ob_gzhandler"); leaving the .htaccess as it was and gave me a bit larger file, 412 kb.
So then it works without the ob_start.
3.Then I also took away the rewriterules etc from .htaccess and it gave me a file of 516 kb.

So I think I mixed it up, is this correct?
the htacces code change the .js to a zipped file that the browser unzip and not the server so no cpu there.

The ob_start("ob_gzhandler"); that I had, gzipped on the fly, therefore gave me a smaller file as also the html was zipped, this the server does and takes cpu.

Yep, I did mix 2 ways of zipping I think.

Should I add something like this in the .htacces file?
Header set Vary Accept-Encoding?
Thanks,
Helen

MickeyRoush

9:54 am on Oct 5, 2011 (gmt 0)

10+ Year Member



I'm no expert, as I'm usually the one asking questions. But I believe you're trying to serve pre-gzipped files (by you) that are already on your server to the user.

This is what I've used in my .htaccess file:

RewriteCond %{HTTP:accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME} !^.+\.gz$
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+) $1.gz [L]


Ant that's all I use. And using firebug, it confirms that the .gz file is being served. I've had to use this on shared servers where mod_gzip or mod_deflate is not set up.

helenp

12:20 pm on Oct 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Mickey,
Its more or less what I use:
RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.js$ $1\.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz

Dont know which is best, but as I understood now its the broswer that unzip the pre-gzipped file, so there is no cpu usage, so its safe to use.
I read that its better to change the filename to .jgz because safari has a bug and dont get .gz
[tutorialajax.com...]
however this is from 2008 and dont know if still the same, also I read that chrome has the same bug.
And to check I did check with chrome and chrome does not serve the gzipped file.....in safari I dont know how to see if it does, however firefox does serve it.

Now the strange thing, as it did not work in chrome I changed the extension .jgz to .gz and uploaded the .gz file, also I changed the extension in .htaccess, and firefox and chrome did not serve the .gz file and the .js file either. And then I tried with your code and the same. Dont know if server blocked .gz files or I should write something else in the .htaccess file to use .gz instead of .jgz.

Edited, just tested jdMorgand code and did not work either..
# If client accepts compressed files
RewriteCond %{HTTP:Accept-Encoding} gzip
# and if compressed file exists
RewriteCond %{REQUEST_FILENAME}.gz -f
# send .html.gz instead of .html
RewriteRule ^(.+\.js)$ /$1.gz [L]

helenp

6:23 pm on Oct 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just discovered the response header in chrome page speed does not deliver any Content-Type on any files....So I dont know if it works or not, and I cant see anywhere to view how many kb the page have in total either...

Also I added the vary in .htaccess as page speed told me to add, dont know if I did it correctly, however it dont say anymore to add it:
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.jgz -f
RewriteRule (.*)\.js$ $1\.js.jgz [L]
AddType "text/javascript" .js.jgz
AddEncoding gzip .jgz
Header append Vary Accept-Encoding

Lol, just dont understand, sometimes I get content-type and content-encoding in chrome and sometimes not, dont seems to make any change if I delete the gzip content in .htaccess or not..
What a mess.

Anybody knows how to check the size of the document in chrome, ie and safari? Think thats the best way to see if works or not.
Please help
Thanks