The following came up a couple of weeks ago, and I thought it would be worth posting for anyone else who hits the same or a similar problem.
When I try to do a build using TFS Build I get this error: TF215085: An error occurred while connecting to Agent \Shopping Catalog\Nightly Build: The request failed with HTTP status 502: Bad Gateway
The solution was to set BypassProxyOnLocal in the registry for TFS on the server (application tier). Note the version number, 9.0, is for TFS 2008. This does not apply to TFS 2005, because the communication with the build agent is different in TFS 2008.
Then run iisreset to make sure that the AT picks up the setting change.
The following post talks about an unrelated topic, but it covers this setting too. The context is normally the client rather than the server, but in this case the server is acting as a client and calling the build agent.