Azure App Service 502, 503 and 504 errors: causes and fixes
Why an Azure App Service app returns 502, 503 or 504: wrong port, failed container start, the 230-second front-end timeout and idle unloading, and how to fix each.
· 6 min read · By the Spot Downtime team
When an app on Azure App Service returns 502, 503 or 504, the error is rarely coming from your code. It comes from the App Service front end, which sits in front of your app and reports that it could not get a good answer from it. The status code tells you which kind of failure to look for.
This guide covers the causes that account for most cases, how to confirm each one with the Azure CLI and the built-in logs, and what to change. The commands use placeholders: replace my-rg and my-app with your resource group and app name.
Start by reading the status code
- 502 Bad Gateway: the front end reached the worker but got an invalid or no response, often because the process crashed or closed the connection.
- 503 Service Unavailable: no healthy instance could take the request. The app is starting, stopped, crashing or out of resources.
- 504 Gateway Timeout: the app accepted the request but did not answer in time. The front end gives up on requests that take longer than about 230 seconds, whatever your own timeout is.
For the general meaning of these codes, see 502, 503 and 504 errors explained. The rest of this post is specific to App Service.
Turn on logs first
Without logs you are guessing. Enable application and web server logging to the file system, then stream them:
az webapp log config -g my-rg -n my-app \
--application-logging filesystem \
--web-server-logging filesystem \
--docker-container-logging filesystem
az webapp log tail -g my-rg -n my-appThe Kudu console at https://my-app.scm.azurewebsites.net also gives you the log files and a shell. For Linux apps, look at the Docker logs (they contain your app's stdout and stderr and the container start messages).
Cause 1: the app listens on the wrong port (Linux and containers)
On Linux, App Service forwards traffic to a single port inside the container. Built-in images set a PORT variable (8080 for Node, for example) that your app should read. For a custom container, App Service tries to detect the port from your EXPOSE instruction. If your app listens somewhere else, the container starts but never answers, and you see 503 or 502.
Tell App Service the port explicitly and make the app read it:
az webapp config appsettings set -g my-rg -n my-app \
--settings WEBSITES_PORT=3000// Node: listen on the port the platform gives you, on all interfaces
const port = process.env.PORT || 3000;
app.listen(port, "0.0.0.0");A second common version of the same mistake is binding to 127.0.0.1 only. The front end connects from outside the container, so the app must listen on 0.0.0.0.
Cause 2: the container takes too long to start
After a deployment or restart, App Service waits for the container to respond before sending traffic. By default it waits 230 seconds. A large image pull, a slow database migration at startup or a cold framework can exceed that, and the platform then reports a failed start and keeps restarting the container.
- Raise the limit with
WEBSITES_CONTAINER_START_TIME_LIMIT(in seconds; the maximum is 1800) if the app legitimately needs longer. - Move migrations and heavy warm-up out of the startup path so the app can answer quickly.
- Keep the image small, so the pull is fast.
az webapp config appsettings set -g my-rg -n my-app \
--settings WEBSITES_CONTAINER_START_TIME_LIMIT=600Cause 3: the app crashes on startup
A missing environment variable, a wrong connection string or a dependency that did not install will make the process exit, and App Service shows 503 while it retries. The log stream shows the exception. Compare your App Service application settings with the variables your app reads. Settings there become environment variables, and they are the usual place a value is missing after a new deployment slot or a copied app.
Cause 4: requests longer than 230 seconds
An endpoint that runs a long report, export or import will hit the front end's idle timeout and return 504, even though your app is still working. This limit is not configurable on the app. Do not stretch the request; change the design:
- Return
202 Acceptedwith a job ID and let the client poll for the result. - Run the work in a background worker, a WebJob or a queue consumer.
- Stream partial output if the client can handle it, since traffic on the connection resets the idle timer.
Cause 5: the app unloads when idle
On plans that support it, the Always On setting keeps the app loaded. Without it, the app is unloaded after a period with no traffic, and the next request pays a cold start. If that start is slow, it shows up as a 502, 503 or a long wait for the first visitor after a quiet spell. The Free and Shared tiers do not offer Always On, so cold starts there are expected.
az webapp config set -g my-rg -n my-app --always-on trueCause 6: out of CPU or memory on the plan
All apps on one App Service plan share its CPU and memory. A busy or leaking app can starve the others, and a worker that runs out of memory is killed and restarted, producing a burst of 502s and 503s. Check the plan's CPU and memory percentage in Azure Monitor around the time of the errors. If both are high, scale the plan up, scale out to more instances, or move the heavy app to its own plan.
Cause 7: unhealthy instances and health checks
Use the health check feature carefully
App Service can ping a path on your app and take instances that keep failing out of the load balancer rotation. This helps when you run two or more instances, because traffic stops going to the bad one. Point it at a light endpoint that checks the things the app truly needs. If the path depends on a database that is down, every instance will fail the check at once, and with one instance there is nothing to rotate to. Keep the endpoint cheap and decide on purpose which dependencies it checks.
Restart, then fix the cause
A restart often brings the app back, and it is a fair first step during an outage:
az webapp restart -g my-rg -n my-appBut if the cause was a wrong port, a missing setting or a memory leak, the error returns. Treat a restart as buying time to read the logs.
Monitor it from outside
The Azure portal tells you what the platform sees. It does not tell you what a visitor sees from another network. An external uptime monitor checks your public URL on a schedule from several regions and alerts you when it returns a 5xx or stops answering, with the status code in the alert. Because cold starts and slow restarts are a common cause of 502 and 503 on App Service, a response-time alert on the same monitor can warn you before visitors see errors.
Quick checklist
Related reading
Get new guides by email
Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.
Keep reading
- HTTP errors · Troubleshooting · ServersPHP-FPM 502 and 504 errors behind nginx: find the cause and fix itnginx returns 502 or 504 when PHP-FPM is down, out of workers or too slow. How to read the logs, tune pm.max_children and use the slow log to find the culprit.November 24, 2026 · 6 min read
- HTTP errors · HTTP · Troubleshooting304 Not Modified: what it means and how conditional requests workA 304 is not an error: it means the cached copy is still good. How ETag and Last-Modified revalidation works, why a 304 can go wrong, and how to debug stale content.November 26, 2026 · 7 min read