Page 1 of 1

Rate-limit nginx to one connection per IP? (downloads)

Posted: Sun Apr 14, 2019 8:58 pm
by VENAXIS
Hello,

I was wondering how would I achieve this through nginx? I have read the nginx wiki but still haven't been able to achieve the desired result.

Is there a way to rate-limit downloads (for all files and subfolders inside of a specific directory) to one file at a time per IP, and also to downgrade to a specific download speed after a specific quota is reached in a day? I saw that all of this was mentioned in the wiki and did try to apply it for days lol but to no avail.

If it's too much trouble, at least I'd want to be able to limit it to one connection per ip and to a specific fixed speed. Your help is greatly appreciated!

Re: Rate-limit nginx to one connection per IP? (downloads)

Posted: Thu Apr 18, 2019 8:41 pm
by dreiggy
Here I scrapped something fast, but not tested... ;) So try to test it before do anything on production...
More info: https://www.nginx.com/blog/rate-limiting-nginx/

/etc/nginx/nginx.conf before:

Code: Select all

    # Wildcard include
    include             /etc/nginx/conf.d/*.conf;
add:

Code: Select all

    # Ratelimit zone
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
Then in custom vhost nginx config you should create something like this:
/home/admin/conf/web/nginx.$DOMAIN.conf-custom and /home/admin/conf/web/snginx.$DOMAIN.conf-custom

add:

Code: Select all

    location /downloads/ {
        limit_req zone=mylimit;
        
        proxy_pass http://my_upstream;
    }
Ensure if here is no errors: nginx -t
And reload nginx: service reload nginx

Re: Rate-limit nginx to one connection per IP? (downloads)

Posted: Fri Apr 26, 2019 11:10 pm
by VENAXIS
dreiggy wrote:
Thu Apr 18, 2019 8:41 pm
Here I scrapped something fast, but not tested... ;) So try to test it before do anything on production...
More info: https://www.nginx.com/blog/rate-limiting-nginx/

/etc/nginx/nginx.conf before:

Code: Select all

    # Wildcard include
    include             /etc/nginx/conf.d/*.conf;
add:

Code: Select all

    # Ratelimit zone
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
Then in custom vhost nginx config you should create something like this:
/home/admin/conf/web/nginx.$DOMAIN.conf-custom and /home/admin/conf/web/snginx.$DOMAIN.conf-custom

add:

Code: Select all

    location /downloads/ {
        limit_req zone=mylimit;
        
        proxy_pass http://my_upstream;
    }
Ensure if here is no errors: nginx -t
And reload nginx: service reload nginx
Hey! Thanks. However I tried a few very similar mothods but I did not get it to work. Do you know how the path for nginx works with a vesta installation (download location)? Let's say it was inside one of my website's public html. I tried full paths before and didn't work. Also the "mylimit", what kind of values does it respond to? Also the proxy_pass. Excuse my ignorance, nginx confuses me and I read the documentation so many times lol

Re: Rate-limit nginx to one connection per IP? (downloads)

Posted: Mon Apr 29, 2019 3:43 pm
by dreiggy
Here is a good tutorial about nginx location blocks. Try to read and understand.
https://www.digitalocean.com/community/ ... algorithms