IIS 502 — Web server received an invalid response while acting as a gateway or proxy server.

Steve Ellwood
3 min readJan 26, 2022

I have a blog post which describes one way to resolve this manually, this is a more robust solution that won’t need further manual intervention

I have a web farm which returned this error too frequently for my liking. I mention the farm as that’s the only place it seems to happen. Shortly after returning this error the site works normally and then the error will recur some time later in the day.

This is how I chose to debug the issue.

The first thing I tried was starting up failed request logs, from inetmgr, click on the site and then the Failed Requests Tracing icon

In order to turn tracing on you need to click Edit Site Tracing

However you may want to click on Add first to add filters for the trace. In my case I wanted all content

I’m only interested in HTTP 502 so I filter to that status

I’m only interested in the web server and left all the areas on for completeness though in my case some of these weren’t needed

Now you basically just need to wait until the issue recurs unless you’re lucky enough to be able to recreate it. In my case I only had to wait an hour or two.

There is a gotcha here. When I went to the View Trace Logs link on inetmgr I got this.

Clearly something is not right. The issue is that I’m running Inetmgr on my machine (you are doing the same right?) and the drive mapping for the location of the failed logs is not available to my laptop. Obviously one solution to this is to put the files on a share or somewhere that other machines can access.

In my case I ended up with this

Which is not really what I hoped for but is a clue (assuming my filters are correct) as it tell us the issue isn’t a WWW one.

The fact that the 502 error appears to resolve itself so quickly and its not producing a failed request log suggests it could be related to the app pool or more specifically that there is some sort of timeout or delay on first connection. IIS Sites have a setting called preloadEnabled. If this is true then the applications can startup automatically without a request e.g. when the app pool starts up.

This setting alone isn’t sufficient though. You also need to turn the app pools start mode to AlwaysRunning.

These two in combination mean that the app pool is always running and so there won’t be any startup delay on your application. Clearly this will mean you may need to review the app pool recycling.

--

--

Steve Ellwood

Senior Integrations Officer at Doncaster Council Any views expressed are entirely my own.