4

i would like to limit website's bandwidth using Nginx in order to share it among multiple websites like i'm able to do in IIS7.

From reading the doc, i found i need to use

limit_req_zone inside http { },

but then, all example are made to limit request rate and not bandwidth, they use $binary_remote_addr, i guess I should use $host instead,

zone=NAME:value , this part is okay.

example uses "rate=value" at the end, but i don't want to limit the connection rate for the website, i want to limit the bandwidth, could i replace it with limit_rate=value ?

Once the zone is setup, i guess i only need to use limit_req at the right place.

Zulgrib
  • 351
  • 5
  • 19

1 Answers1

2

You can either user the limit_rate directive or set the $limit_rate variable in the right contexts (cf. docs).

Here is an example with the variable:

http {
    map $host $limit_rate {
        example.org 0;
        example.com 1m;
        default 4k;
    }

    server {
        listen 80;
    }
Bernard Rosset
  • 1,373
  • 12
  • 25
  • But `limit_rate` is per request, not per website. I got the impression that the poster wants to limit bandwidth for a whole website = server block. If many people open many connections, `limit_rate` wouldn't restrict the _total_ bandwidth used by the website. It'd grow proportionally to the number of people. (Or am I missing something?) – KajMagnus Jul 01 '16 at 05:16
  • That is the usual use. However, the key on which requests are counted is defined in the configuration, with the help of the [`limit_req_zone`](http://nginx.org/en/docs/http/ngx_http_limit_req_module.html#limit_req_zone) directive. As configured in my answer, each request to each different value of $host adds a drop to the corresponding bucket, thus requests-limiting $host. – Bernard Rosset Jul 01 '16 at 13:22
  • But this is about bandwidth, not about request limiting. I wrote about `limit_rate` (and that's the variable you set in the answer text too), which limits bytes per second, i.e. bandwidth. — In your comment, you wrote `limit_req_zone` and about limiting _requests_ per second instead — so I'm thinking that there's a misunderstanding. You're writing about limiting requests per second, and I about bandwidth per second. — I don't think there's any zone directive that can be used together with `limit_rate`. Only for `limit_req`. I.e. there's no `limit_rate_zone`, only a `limit_req_zone`. – KajMagnus Jul 01 '16 at 19:44
  • Oh you are right I got mixed up in my previous comment... My original answer still stands and is the best nginx can do. If you want simpler bandwith limitation (not per-request), I suggest you use a layer3/4 software to do so, such as iptables on GNU/Linux. You would however need to isolate hosts on dedicated IP addresses (easily doable with IPv6 blocks, for IPv4, it depends if you got access to several ones) – Bernard Rosset Jul 02 '16 at 13:03