I stumbled across an issue that I had trouble fixing. It was a 502 bad gateway when I tried to open the media library. The ajax call gave the error. So when I searched for this problem I could not find a solution.
But after a lot of trial and error it happened to be too many images. Over 10.000 records in the database. Even when I had the image files removed the error persisted. So ended up removing all attachment files in the database too.
Now the ajax call didn’t give me a 502 bad gateway. So it really had to do with the amount of images. Then I saw this question / answer: https://wordpress.stackexchange.com/questions/135960/wordpress-admin-ajax-php-returns-502-bad-gateway