Running Sanic using gunicorn and nginx, getting 502 bad gateway error

Hi, I am currently running sanic server using gunicorn(4 workers) and Nginx. As the number of concurrent users increases request failures start increasing with the message (502 Bad Gateway error).

I have also tried out the Nginx load balancer with 2 instances of 8GB RAM, 4vCPU ubuntu configuration but still getting the same error.

My first question is why are you using gunicorn as your server? It exists as a compat layer if there are some gunicorn features nor available elsewhere. Outside of this, I would heavily suggest either using sanic in ASGI mode with uvicorn or hypercorn, or using the built in Sanic server (which will provide you with the most performance).

How long are your response times?

I need to scale my application thats why I used gunicorn workers. Is ASGI mode provides that much level of scalability?

Tried uvicorn still getting same the error.
*1003873 connect() failed (111: Connection refused) while connecting to upstream, client
and 502 Bad gateway.
Unable to use ASGI mode as I need to create a endpoint during the runtime.
Please help me out.

Or maybe you can just use Sanic server with Nginx upstream to scale out?