Saturday, June 30, 2018

Proxy Limiting


Introduction
------------

- Nginx and Nginx Plus can limit the ff:
    a. number of connections per key value (e.g per IP address)
    b. request rate per key value (limits requests per second or minute)
    c. download speed

Limiting Number of Connections
------------------------------

setup
http {
    limit_conn_zone $server_name zone=servers:10m;
    # $server_name -> key
    # zone         -> used by worker processes to share counters for key values
    # servers      -> zone name
    # 10m          -> zone size
 [...]

  # limits connection on this location
  location /download/ {
      limit_conn addr 1;
  }
} 

Limiting Request Rate
---------------------

setup
http {
  [...]
    # limits to 1 request per second (r/m for request per minute)
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 [...]

  location /download/ {
      # This will limit requests to this location by 1r/s. If requests
      # exeeds rate limit, those are put into queue. `burst` parameter
      # is the max requests that awaits processing. If requests exceeds
      # `burst` value, Nginx will respond a 503 error.
      limit_req zone=one burst=5;

      # You can use this instead to prevent delay set by `burst`
      # limit_req zone=one burst=5 nodelay;
  }
}

Limiting bandwidth
------------------

limits dowload speed to 50k/s
but multiple connections are
still allowed
  location ~ \.bin {
    root /data;
    limit_rate 50k;
  }
limits download speed and at
the same time limits 1 connection
per IP address
http {
  [...]
  limit_conn_zone $binary_remote_addr zone=addr:10m;
  [...]
}

server {
  [...]
  location ~ \.bin {
    root /data;
    limit_conn addr 1;
    limit_rate 50k;
  }
  [...]
}
limits bandwidth after certain size
has been downloaded (e.g: useful
when client needs to download a
file header)
location {
  [...]
  limit_rate_after 500k;
  limit_rate 20k;
  [...]
}

No comments:

Post a Comment