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.
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:
wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-stable_current_amd64.deb dpkg -i mod-pagespeed-*.deb
now remove the .deb and restart the Apache service
rm mod-pagespeed-*.deb /etc/init.d/apache2 restart
When installed (or if already enabled by your share hosting provider), simply copy/paste the following in your .htaccess (or virtualhost)
<IfModule pagespeed_module> ModPagespeed on ModPagespeedRewriteLevel CoreFilters </IfModule>
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:
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:
<IfModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 30 seconds" </IfModule>
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…..so 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:
BandWidthModule On ForceBandWidthModule On BandWidth 192.168.0.0/24 0 Bandwidth all 100000 LargeFileLimit * 8000 50000
This will force the Bandwidth control, set no limitation is the traffic is coming from 192.168.0.xxx (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:
LargeFileLimit .avi 1 30000 LargeFileLimit .mp3 1000 10000
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:
<IfModule mod_ratelimit.c> <Location /downloads> SetOutputFilter RATE_LIMIT SetEnv rate-limit 100 </Location> </IfModule>
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:
MaxConnection all 2
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!