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.

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\TeamFoundation\RequestSettings]

"BypassProxyOnLocal"="true"

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.
http://blogs.msdn.com/buckh/archive/2007/09/17/tfs-2008-controlling-the-number-of-threads-used-in-uploading-and-downloading-files.aspx