heroku-load-balancer icon indicating copy to clipboard operation
heroku-load-balancer copied to clipboard

Set ratelimit

Open gaudinjeremy opened this issue 2 years ago • 1 comments

Hi,

first of all thank you for this package it's exactly what I was looking for for my apps.

I need to specify a ratelimit and custom error pages in the load balancer.

I succeeded in setting up the ratelimit in the "constants.py" file.

`limit_req_zone $binary_remote_addr zone=limit:20m rate=10r/m;

server <
    listen {port};

    location / <

        limit_req zone=limit burst=5 nodelay;
        limit_req_status 429;

        proxy_pass http://main;
        proxy_next_upstream error timeout invalid_header http_502 http_503 http_504;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host;
    >`

On the other hand, after several tries I can't display my specific page if the ratelimit is reached.

In my Nginx configuration I had this

error_page 429 =429 /error_429.json; location = /error_429.json { root <%= ENV['HOME'] %>; }

==>

error_page 429 =429 /error_429.json; location = /error_429.json < root /; >

I tried several things but I think it is the root that is not working. How to get the variable ENV HOME from Heruku so that it points where it should.

I added the file error_429.json in the src folder and in the root of the project.

Sorry I'm not comfortable with Python at all.

Thanks

gaudinjeremy avatar May 25 '23 09:05 gaudinjeremy

It seems that I found a solution, not sure if it is the right one but adding this in the "constants.py" file it works

import os
env_home = os.getenv("HOME")

NGINX_LOAD_BALANCER_CONFIG_TEMPLATE = """events <
    worker_connections 4096;
>

AND

location = /error_429.json <
    root """+env_home+""";
>

This takes into account the files located in the root of the project.

gaudinjeremy avatar May 25 '23 09:05 gaudinjeremy