Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Frontend fetch gets error - too many requests

default discord avatar
arctomachine3 years ago
16

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 requests

status 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

  • default discord avatar
    neonpinkvisu3 years ago

    You should maybe take a look at Rate Limit in your payload backend

    https://payloadcms.com/docs/production/preventing-abuse#rate-limiting-requests

    at 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



    https://payloadcms.com/docs/rest-api/overview
  • discord user avatar
    jacobsfletch
    3 years ago

    Yea you

    could

    increase 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

    @397630111362580484

    suggested



    I just opened a PR to surface the rate limit defaults in the documentation,

    https://github.com/payloadcms/payload/pull/1520
  • default discord avatar
    arctomachine3 years ago

    The 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.

  • discord user avatar
    jacobsfletch
    3 years ago

    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

  • default discord avatar
    arctomachine3 years ago

    Do you think letting nginx do the rate limiting instead would be a good solution?

  • discord user avatar
    jacobsfletch
    3 years ago

    Have you tried

    trustProxy: true

    in your

    rateLimit

    config like that blockquote points out?

  • default discord avatar
    arctomachine3 years ago

    I enabled it, will see if it helps



    Yes. I still get this error if I try to quickly and chaotically click links

  • discord user avatar
    jmikrut
    3 years ago

    one other thing you might try in your nginx server block is to add this:



                    proxy_set_header X-Forwarded-For $remote_addr;
  • default discord avatar
    arctomachine3 years ago

    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;
    }
  • discord user avatar
    jacobsfletch
    3 years ago

    Try moving

    proxy_set_header

    into 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;
            }
    }
  • default discord avatar
    arctomachine3 years ago

    For both front and back servers?

  • discord user avatar
    jacobsfletch
    3 years ago

    yea in both



    although you shouldn't need the

    X-Forwarded-For

    in the front-end

  • default discord avatar
    arctomachine3 years ago

    Could this issue be caused by using http v2? I have

    listen 443 ssl http2;

    this inside server block instead of 1.1

  • discord user avatar
    jacobsfletch
    3 years ago

    That is a good question

  • default discord avatar
    arctomachine3 years ago

    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

  • discord user avatar
    jmikrut
    3 years ago

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.