Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a requirement to run an HTTP server that rejects new HTTP requests (with a 503, or similar) when the global transfer rate of current HTTP responses exceeds a certain level. For example, if the web server is transferring at 98Mbps, and a new HTTP request arrives, we would want to reject this (as we couldn't guarantee a good speed).

I've had a look at mod_cband for Apache, limit_req for nginx, and lighttpd's rate limiting features, but none of them seem to handle my (rather contrived, granted) use case.

I should add that I'm open to using pretty much any web server, and am open to implementing this in iptables rules if someone can craft such a rule! (Refusing the TCP connection is fine, it doesn't have to respond with an HTTP 503).

Any suggestions?

share|improve this question

2 Answers 2

iptables accounting script that writes to a file when bandwidth is too high. mod_rewrite rule that reads a map using rewritemap, map file would contain

blocked blocked

(lhs and rhs required)

In your <VirtualHost> container:

RewriteMap blockmap txt:/path/to/file/map.txt

In your .htaccess (or <VirtualHost>)

RewriteEngine on
RewriteCond ${blockmap:blocked|NOTFOUND} ^blocked$
RewriteRule .*  http://www.google.com/ [R=301,L]

Rather than redirect, hand them a come back later page, serve it as an error 503, etc.

share|improve this answer
    
The following can help write the "traffic is too high" file: I was able to evaluate the bandwidth utilization by parsing the output of 'ifconfig eth0' (or one of those /proc counters) - this reports total bytes send and recived. I simply store the old value in a file and subtract it from the current. I run this script every minute from Cron. –  Aleksandr Levchuk Dec 30 '10 at 1:55

To do this, you can setup a Haproxy in front of your Apache. To configure the rate limit in Haproxy read this excellent post from Kyle Brandt

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.