Faster WebsitesFollowing on from last week I've been working on more ways to make websites faster and save precious bandwidth. gzipI've enabled gzip compression in apache. I've set it to compress text based files like Javascript, CSS and HTML. There is a trade-off between cpu and bandwidth, but so far it seems worth it. Most files compress to around 30% of their original size. It is not worth the server load to compress images as jpegs/ gifs and pngs are already a compressed format. I've also enabled gzip compression in PHP. Because of shiftlib this was easy to add to multiple sites at the same time. <? ob_start("ob_gzhandler"); ?> To enable gzip in apache you need to do the following: # nano /etc/httpd/conf.d/mod_deflate.conf
Then restart apache:
Expiration headersI've set apache to send 30 day cache headers with images/ css/ and javascript. This should mean that these files are cached on the users browser so that they don't have to download them on every single page load. This could result in a significant saving in bandwidth as images especially account for a big proportion for a web page. To enable expiration headers in apache - you need to do the following:
#nano /etc/httpd/conf.d/mod_expires.conf
Then restart apache:
Caching thumbnailsUp until now my main PHP thumbnail script has generated images on the fly. PHP is so fast that it doesn't take very long even for a page full of images. However when you have lots of visitors all those clock cycles add up and there are significant savings to be made. My thumbnail script works by pointing the image source to a php script - and passing in the filename as a variable, e.g
<img src="thumb.php?f=example.jpg"> There is a big problem with this. Because it's pointing to a php script the cache headers won't work. You could use the php header function to send the correct cache headers but I still suspect that it won't cache as well as a regular image. There is another problem - search engines won't know that it's an image - especially if it's in a link. They would just think it's a regular php page. So they will go ahead and pull down a bunch of images without needing to. So my new approach is a little different. I have a function that checks if there is a cached thumbnail before creating one if necessary. It then returns the image tag of the resulting thumnail. e.g. <? image('example.jpg'); ?> outputs: <img src="uploads/cache/example.jpg"> Search enginesI checked the web-stats of some of the busier websites that I host. There was a significant chunk of bandwidth being used by search engines - around 10%. Search engines will index an entire site several times over a month - but they shouldn't have to pull down every single image. This may be related to the thumbnail issue outlined above but I also have taken another precaution. I've created a robots.txt file and am now preventing access to uploads folders and thumbnail scripts and anything else that a search engine doesn't need. I may need to add an exception for google image search, but for the time being I will monitor it and see how it goes. 18/07/2010 permalink | Posted in web development | 0 Comments » Leave a reply |
About meAdam Jimenez is a freelance web developer who has been professionally developing websites since 2000.Find me
Projects
Archive |