Azure App Service gives 504 - Gateway Timeout Issue

Dinesh Kumar Sai M 0 Reputation points
2025-03-05T05:49:35.6466667+00:00

I am hosting APIs with App Service. The API backend is build with Python Fast API Framework, the configuration:

  • OS : Linux
  • Stack : Python
  • Major Version : Python - 3

Minor Version : Python - 3.12

User's image

Its running fine for some hours while started/restarted, but after sometime its giving 504 gateway timeout error. Again needs to restart to host the API. The API needs to serve for all the time, no timeout should be set.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,933 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alekhya Vaddepally 1,670 Reputation points Microsoft External Staff Moderator
    2025-03-05T12:08:54.9333333+00:00

    Hi Dinesh Kumar Sai M,

    The 504 Gateway Timeout error in Azure App Service usually happens when the application doesn't respond within the set timeout period.

    This is common when hosting APIs. Here are some solutions may help keep your responsive and reduce 504 gateway time out errors

    Need to check azure app service have enough resources like having basic (b1, b2), standard (s1, s2) tier, and also check your CPU memory usage in Azure Monitor to check if resource limits are causing the issue.

    Azure App Service may scale down or unload your app if it's inactive. To avoid this, you can also enable “Always on” in Azure portal> App Service >Configuration > General Settings

    You can also increase timeout by default 20 seconds in azure application gateway by configuring the backendhttpsettings.

    Use the AutoHeal feature to recycle automatically the worker process based on triggers, which can help with memory leaks

    If your API calls are taking too long, consider optimizing database queries, caching responses, or using background tasks in Fast API.

    Use Azure Application Insights to check which API calls are slow

    https://learn.microsoft.com/en-us/azure/application-gateway/application-gateway-troubleshooting-502#request-time-out

    https://learn.microsoft.com/en-us/azure/frontdoor/troubleshoot-issues#http-504-error-gateway-timeout

    https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-http-502-http-503#troubleshoot-http-errors-of-502-bad-gateway-and-503-service-unavailable-in-azure-app-service

    If the answer is helpful, please click Accept Answer and kindly upvote it so that other people who faces similar issue may get benefitted from it.

    Let me know if you have any further Queries.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.