Mastering Nginx

(Ron) #1
Chapter 6

[ 131 ]

http {

limit_req_zone $binary_remote_addr zone=requests:10m rate=1r/s;

limit_req_log_level warn;

server {

limit_req zone=requests burst=10 nodelay;

}

}

Using $binary_remote_addr
We use the $binary_remote_addr variable in the preceding
example to know exactly how much space storing an IP address
will take. This variable takes 32 bytes on 32-bit platforms and
64 bytes on 64-bit platforms. So the 10m zone we configured
previously is capable of holding up to 320,000 states on 32-bit
platforms or 160,000 states on 64-bit platforms.

We can also limit the bandwidth per client. This way we can ensure that a few


clients don't take up all the available bandwidth. One caveat, though: the limit_


rate directive works on a connection basis. A single client that is allowed to open
multiple connections will still be able to get around this limit:


location /downloads {

limit_rate 500k;

}

Alternatively, we can allow a kind of bursting to freely download smaller files,


but make sure that larger ones are limited:


location /downloads {

limit_rate_after 1m;

limit_rate 500k;

}
Free download pdf