502 Bad Gateway - Unable to rebuild

You could try to add to your rebuild command the --skip-prereqs flag (but be sure to have downloaded a backup and your app.yml beforehand, just in case)