Skip to main content

Improve performance of your website and limit bandwidth used

I received recently a question from Armen, a reader on a shared hosting, asking how to limit the bandwidth used of a website.

In my old Hosting Manual from A to Z, I had described some of the steps before but never went much further. So here I am today.

You are looking to speed up your website? Or limit the download speed of one of your web gallery? Here are few apache modules to do so.

I’m assuming you have either access to your virtualhost, or to the .htaccess of your website.

Gzip Compression

One of the traditional way to improve the performance is to compressed files to reduce the HTTP response time. We usually use Gzip compression to zips HTML documents, scripts, etc… Basically anything not too CPU intensive and that will improve the loading speed. (So no need to compress PDF, zip files, etc…).

At the same time than improving the speed to display your pages, it will reduce the server bandwidth. The trade off is a little more CPU load (But usually, it is negligible)
There are nowadays 2 popular modules to do it:
The very powerful mod_pagespeed, developed not too long ago by Google, that applies  performance best practices to your website. However not all Shared hosting got this module enabled (Better to check with them)
– If you have your own server (let’s say Debian/Ubuntu 64b), you simply need to install their .deb.
In root, run:

now remove the .deb and restart the Apache service

When installed (or if already enabled by your share hosting provider), simply copy/paste the following in your .htaccess (or virtualhost)

That will activate the default Rewrite level (CoreFilters) that includes a set of filters that Google team believes are safe for production server.There is actually the following list of filters:

If you want to add more, you can then simply add

Some websites recommend to add:

If you want to know more about mod_pagespeed, DigitalOcean has a very good tutorial.

– If you are on a shared hosting, not sure your provider support it. Better to check with them….if not, you can still use the mod_deflate.
In your .htaccess, add:

Browser Caching

2 modules are usually used to force the visitor’s browser to cache the website and are usually enabled in most of the shared hosting. It’s the modules expires and headers.
I’m more interested in the mod expires, so I will only talk about this one, that is good enough for me.

This will significantly improve the experience of your visitors (Except if it is one time shot of course), however note that caching is not for website with very dynamic content, but for most of the blogs, galleries, etc… it doesn’t change much.If you have your own server, you will need to enable it first.

In root, simply run:

And restart Apache

Now, configure your virtualhost or .htaccess as the following:

This will cache all the content of your website for 30 seconds.
But most probably you want to better tweak this as some pictures, pdf, etc…does not need to be retrieved for a long time or directly having a much longer cache if you don’t plan to change your website frequently.

For example, instead you could have:

A long cache period, if you don’t plan to make much dynamic change to your website.Make sure when you make any modification, to restart Apache.

Limit the bandwidth

Let’s say now, you have a web gallery, like based on Piwigo or Lychee and you want to limit the bandwidth that your visitors are using.

– You will need to have the mod bandwidth activated. Probably already enabled. If not, simply run:

However, this mod seems not to be working with .htaccess… for shared hosting, it won’t be able to do it that way, you will need the mod_ratelimit instead. (Less advanced tho)

For those with dedicated server or VPS, in your virtualhost, add the following:

This will force the Bandwidth control, set no limitation is the traffic is coming from (On your local network for example), will fix a limit of 100kb/s for all type of content and a limit of 50kb/s for files larger than 8mb.

If you want to better customize those values, you can also split per filetype like this example:

all your .avi files >1kb will have a limit of 30kb/s while your MP3 >1mb will have a limit of 10kb/s.

And for those with only access to .htaccess, you will need the mod_ratelimit and in our .htaccess, add:

This will limit the download speed to 100kb/s on the content located under /downloads

– If you want to limit the maximum concurrent connections per IP (Like to block the download accelerator type of program, or avoid someone is using all your bandwidth for himself), you can simply add:

Once again, after your modification, restart Apache.

If you are looking for something more advanced with quota, maximum of connection per seconds, etc… I recommend you to check the mod_cband.

Hope all these will help you speed up your website and save some of your bandwidth.
If you know better way yet still easy to tweak Apache to improve the performance, please share them in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *