piefedadmin on Nostr: Fediverse traffic is pretty bursty and sometimes there will be a large backlog of ...
Fediverse traffic is pretty bursty and sometimes there will be a large backlog of Activities to send to your server, each of which involves a POST. This can hammer your instance and overwhelm the backend’s ability to keep up. Nginx provides a rate-limiting function which can accept POSTs at full speed and proxy them slowly through to your backend at whatever rate you specify.
For example, PieFed has a backend which listens on port 5000. Nginx listens on port 443 for POSTs from outside and sends them through to port 5000:<code>upstream app_server { server 127.0.0.1:5000 fail_timeout=0;}</code><code>server { listen 443 ssl; listen [::]:443 ssl; server_name piefed.social www.piefed.social; root /var/www/whatever; location / { # Proxy all requests to Gunicorn proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass
http://app_server; ssi off; }</code>
To this basic config we need to add rate limiting, using the ‘limit_req_zone’ directive. Google that for further details.<code>limit_req_zone $binary_remote_addr zone=one:100m rate=10r/s;</code>
This will use up to 100 MB of RAM as a buffer and limit POSTs to 10 per second, per IP address. Adjust as needed. If the sender is using multiple IP addresses the rate limit will not be as effective. Put this directive outside your server {} block.
Then after our first location / {} block, add a second one that is a copy of the first except with one additional line (and change it to apply to **location /inbox** or whatever the inbox URL is for your instance):<code>location /inbox { <strong>limit_req zone=one burst=300;</strong># limit_req_dry_run on; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass
http://app_server; ssi off; }</code>
300 is the maximum number of POSTs it will have in the queue. You can use limit_req_dry_run to test the rate limiting without actually doing any limiting – watch the nginx logs for messages while doing a dry run.
It’s been a while since I set this up so please let me know if I mixed anything crucial out or said something misleading.
https://join.piefed.social/2024/04/17/handling-large-bursts-of-post-requests-to-your-activitypub-inbox-using-a-buffer-in-nginx/#nginx #webPerformance
Published at
2024-04-17 08:53:24Event JSON
{
"id": "597a5cf41435cd0291749e3325212c969a4f1f689b189f00701680549841d2bf",
"pubkey": "bcab87f8e4c3c856bd92fc261528bf6c99870e77acf3ca54168483ba35946e3e",
"created_at": 1713344004,
"kind": 1,
"tags": [
[
"t",
"webPerformance"
],
[
"t",
"nginx"
],
[
"proxy",
"https://join.piefed.social/2024/04/17/handling-large-bursts-of-post-requests-to-your-activitypub-inbox-using-a-buffer-in-nginx/",
"web"
],
[
"proxy",
"https://join.piefed.social/2024/04/17/handling-large-bursts-of-post-requests-to-your-activitypub-inbox-using-a-buffer-in-nginx/",
"activitypub"
],
[
"L",
"pink.momostr"
],
[
"l",
"pink.momostr.activitypub:https://join.piefed.social/2024/04/17/handling-large-bursts-of-post-requests-to-your-activitypub-inbox-using-a-buffer-in-nginx/",
"pink.momostr"
]
],
"content": "Fediverse traffic is pretty bursty and sometimes there will be a large backlog of Activities to send to your server, each of which involves a POST. This can hammer your instance and overwhelm the backend’s ability to keep up. Nginx provides a rate-limiting function which can accept POSTs at full speed and proxy them slowly through to your backend at whatever rate you specify.\n\nFor example, PieFed has a backend which listens on port 5000. Nginx listens on port 443 for POSTs from outside and sends them through to port 5000:\u003ccode\u003eupstream app_server {\u0026nbsp;\u0026nbsp;\u0026nbsp;server 127.0.0.1:5000 fail_timeout=0;}\u003c/code\u003e\u003ccode\u003eserver {\u0026nbsp;\u0026nbsp;\u0026nbsp;listen 443 ssl;\u0026nbsp;\u0026nbsp;\u0026nbsp;listen [::]:443 ssl;\u0026nbsp;\u0026nbsp;\u0026nbsp;server_name piefed.social www.piefed.social;\u0026nbsp;\u0026nbsp;\u0026nbsp;root /var/www/whatever;\u0026nbsp;\u0026nbsp;\u0026nbsp;location / {\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;# Proxy all requests to Gunicorn\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header X-Forwarded-Proto $scheme;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header Host $http_host;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_redirect off;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_http_version 1.1;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header Connection \"\";\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_pass http://app_server;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;ssi off;\u0026nbsp;\u0026nbsp;\u0026nbsp;}\u003c/code\u003e\n\nTo this basic config we need to add rate limiting, using the ‘limit_req_zone’ directive. Google that for further details.\u003ccode\u003elimit_req_zone $binary_remote_addr zone=one:100m rate=10r/s;\u003c/code\u003e\n\nThis will use up to 100 MB of RAM as a buffer and limit POSTs to 10 per second, per IP address. Adjust as needed. If the sender is using multiple IP addresses the rate limit will not be as effective. Put this directive outside your server {} block.\n\nThen after our first location / {} block, add a second one that is a copy of the first except with one additional line (and change it to apply to **location /inbox** or whatever the inbox URL is for your instance):\u003ccode\u003elocation /inbox {\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u003cstrong\u003elimit_req zone=one burst=300;\u003c/strong\u003e# \u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;limit_req_dry_run on;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header X-Forwarded-Proto $scheme;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header Host $http_host;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_redirect off;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_http_version 1.1;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_set_header Connection \"\";\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;proxy_pass http://app_server;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;ssi off;\u0026nbsp;\u0026nbsp;}\u003c/code\u003e\n\n300 is the maximum number of POSTs it will have in the queue. You can use limit_req_dry_run to test the rate limiting without actually doing any limiting – watch the nginx logs for messages while doing a dry run.\n\nIt’s been a while since I set this up so please let me know if I mixed anything crucial out or said something misleading.\n\nhttps://join.piefed.social/2024/04/17/handling-large-bursts-of-post-requests-to-your-activitypub-inbox-using-a-buffer-in-nginx/\n\n#nginx #webPerformance",
"sig": "7293c0f1f41ef20be48a16836f8d849cd53bae948c5cd4a20e1390c22cb7aaf7487b39abebec38214149b34f5fb96be584864ad5a2713007cc7c777cf6f504ed"
}