Backport patches from another fork related to BadGateway checks.