We've been fighting to find the solution to random and frequent 502 Bad Gateway errors on many of our websites on one of our servers. They would generate entries like this in the site's proxy_error_log:
2016/06/17 08:55:46 [error] 23668#0: *24939 upstream prematurely closed connection while...