Infinite loop behind SSL proxy on non-standard port

Changing

proxy_set_header Host $host;

to

proxy_set_header Host $http_host;

fixed the issue.

(source)