Nginx Proxy throws HTTP 504 Gateway Timeout

we are using Nginx as reverse proxy which takes requests and direct requests to the service pods. Under load we were seeing some of the transactions are failing with HTTP 504 Error . Is there a way to prevent this error . The Nginx we were using is 1.15.8 .