How would you diagnose and resolve a scenario where an application works correctly when run directly with Kestrel locally but returns 502 Bad Gateway errors when deployed behind IIS or nginx in production?
Short answer
Common causes include: the reverse proxy's own configuration not correctly forwarding to Kestrel's actual listening port/address (mismatched port bindings), Kestrel crashing/failing to start due to a missing configuration only present via IIS/proxy environment (like a missing…
Advertisement
AModel answer
Common causes include: the reverse proxy's own configuration not correctly forwarding to Kestrel's actual listening port/address (mismatched port bindings), Kestrel crashing/failing to start due to a missing configuration only present via IIS/proxy environment (like a missing certificate), the ASP.NET Core Module (for IIS) not being installed or an outdated hosting bundle, or the app failing to bind because ASPNETCORE_URLS conflicts with what the proxy expects -- diagnosing typically starts with checking the application's own stdout/stderr logs (enabled via <aspNetCore stdoutLogEnabled="true" /> for IIS) to see if the app is crashing on startup entirely, rather than a genuine proxy misconfiguration.
<!-- web.config -- enable stdout logging to diagnose startup failures -->
<aspNetCore processPath="dotnet" arguments=".\MyApp.dll"
stdoutLogEnabled="true"
stdoutLogFile=".\logs\stdout" />
# Check Windows Event Viewer's Application log for ASP.NET Core Module errors
# Check IIS's own HTTP error logs for the specific 502.x sub-status code, which narrows the causeReal-world example
A team debugging a production 502 error behind IIS discovers via enabled stdout logging that the app was actually crashing on startup due to a missing environment-specific configuration value only set in production, a root cause invisible from IIS's generic 502 error page alone.
Likely follow-ups
- What do the different IIS 502.x sub-status codes indicate about the specific failure type?
- How does this diagnostic approach differ when the reverse proxy is nginx instead of IIS?
Related topics