Improve performance of your website and limit bandwidth used

22nd Oct 2014 Karibu

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:

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:

add_head
combine_css
convert_jpeg_to_progressive
convert_meta_tags
extend_cache
flatten_css_imports
inline_css
inline_import_to_link
inline_javascript
rewrite_css
rewrite_images
rewrite_javascript
rewrite_style_attributes_with_url

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

ModPagespeedEnableFilters filter1,filter2,filter3

Some websites recommend to add:

ModPagespeedEnableFilters extend_cache,combine_css,combine_javascript,collapse_whitespace,move_css_to_head

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:

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript
</IfModule>

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:

a2enmod expires

And restart Apache

/etc/init.d/apache2 restart

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:

<IfModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 5 minutes"
 ExpiresByType image/jpg "access plus 1 month"
 ExpiresByType image/jpeg "access plus 1 month"
 ExpiresByType image/gif "access plus 1 month"
 ExpiresByType image/png "access plus 1 month"
 ExpiresByType text/css "access 1 month"
 ExpiresByType text/html "access 1 week"
 ExpiresByType application/pdf "access 1 year"
 ExpiresByType text/x-javascript "access 1 month"
 ExpiresByType image/x-icon "access 1 year"
 </IfModule>

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:

a2enmod bw

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.

/etc/init.d/apache2 restart

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!

Gravatar

Loves to discover web-based apps to install on his own server@home and write articles about it

0 Comments:

Add a comment