My frontend runs multiple fetch requests (1-6 rest api) for each visited page. And sometimes it gets response with non-200 status and
too many requestsstatus text.
Is my backend responsible for this error or is it problem somewhere else in my stack?
My setup:
- nginx as reverse proxy
- next.js as frontend (v13, app folder, server components)
- payload as backend (v1.2.0)
- mongodb on remote server at clever-cloud.com
You should maybe take a look at Rate Limit in your payload backend
https://payloadcms.com/docs/production/preventing-abuse#rate-limiting-requestsat least that's what I did when I had a problem with too many requests... But my solution was to delete the requests from my for-loops and have something like a cache... don't know if that solves the problem
Yea you
couldincrease your rate limit, but it sounds like your front-end is simply making too many requests. Looking at the source code, the default is 500 requests in a 15 minute window. Double check your code for a loop like
@397630111362580484suggested
I just opened a PR to surface the rate limit defaults in the documentation,
https://github.com/payloadcms/payload/pull/1520The article says this behavior is to be expected with reverse proxy
Very commonly, NodeJS apps are served behind nginx reverse proxies and similar. If you use rate-limiting while you're behind a proxy, all IP addresses from everyone that uses your API will appear as if they are from a local origin (127.0.0.1), and your users will get rate-limited very quickly without cause. If you plan to host your app behind a proxy, make sure you set trustProxy to true.But it still feels strange. Next tries to cache all images (and for a long time), and I set 1 minute caching for data fetch requests. There are only 16 different endpoints, which together are half of 33.3 allowed requests per minute.
But then there are dynamic routes too, which need to be fetched independently from each other.
So is it maybe a flaw in my design rather than hitting proper technical limitation?
And I never get this error on local machine. It only happens in production, where nginx is used, even if I am the only visitor.
We use nginx as a reverse proxy for nearly all of our Payload apps, without problem
We rarely modify nginx defaults, but you may need to
Otherwise I think your right, it sounds like an implementation detail in your project
Do you think letting nginx do the rate limiting instead would be a good solution?
Have you tried
trustProxy: truein your
rateLimitconfig like that blockquote points out?
I enabled it, will see if it helps
Yes. I still get this error if I try to quickly and chaotically click links
one other thing you might try in your nginx server block is to add this:
proxy_set_header X-Forwarded-For $remote_addr;Like this?
#backend
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://payload_upstream;
}#frontend
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
location / {
proxy_pass http://nextjs_upstream;
}Try moving
proxy_set_headerinto the
/location block, like this:
server {
server_name YOUR_DOMAIN_NAME_HERE; // i.e. cms.yourdomain.com
location / {
proxy_pass http://localhost:YOUR_PORT_NUMBER_HERE; // i.e. 8000
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $remote_addr;
}
}For both front and back servers?
yea in both
although you shouldn't need the
X-Forwarded-Forin the front-end
Could this issue be caused by using http v2? I have
listen 443 ssl http2;this inside server block instead of 1.1
That is a good question
This header did not help, error again
This time I tested it with another person, we just casually browsed the site, not even attempting to navigate as many pages as possible
did you restart your nginx server?
if you're the only user navigating though, and still seeing this error, i would expect that it might not be related to this after all and that your code might just be making too many requests, too frequently, like stated above
Star
Discord
online
Get dedicated engineering support directly from the Payload team.