Skip to content

Commit

Permalink
Limit connections which result in too many 4xx & 5xx statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Jan 18, 2024
1 parent 5276f84 commit 7ddbf91
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions etc/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ http {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

# limit HTTP requests based on URL path
map $request_uri $limit_key {
default "";
~^/accounts/ $binary_remote_addr;
}
limit_req_zone $limit_key zone=ten-per-sec:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=one-per-sec:10m rate=1r/s;
limit_req_status 429;

upstream kiwitcms {
Expand Down Expand Up @@ -97,8 +99,15 @@ http {
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass kiwitcms;
uwsgi_intercept_errors on;
# respond with the same original error status/response and redirect for rate limiting
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 = @error_handler;

limit_req zone=ten-per-sec burst=20 nodelay;
}

location @error_handler {
limit_req zone=one-per-sec burst=2; nodelay;
}
}
}

0 comments on commit 7ddbf91

Please sign in to comment.