Zend certified PHP/Magento developer

Magento2 on custom port with reverse proxy – redirection loop

When I try to install Magento on local configuration with Docker, I get into trouble when trying to use custom port for HTTP.
Frontend part works with no problems, but trying to load admin page ends up with redirection loop.

Every configuration is tested on newly installed Magento, fresh database and cleaned cache.

Configurations that work:

  • localhost or custom local domain -> Proxy container (:80) -> App container (:80 or different)
  • localhost or custom local domain -> App container (:80)
  • localhost or custom local domain -> App container (:8080)

Configuration that doesn’t

  • localhost or custom local domain -> Proxy container (:8080) -> App container (:80 or different or even :8080)

My NGINX proxy configuration includes proxy headers

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host   $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port   8080; #or 80 accordingly
proxy_set_header X-Forwarded-Proto  http;

It redirects to itself, but everytime it passes new key in the URL
http://localhost:8080/admin/admin/index/index/key/c65aa1655754fdbd042602746f405e91bc1bf2e67e0532b15e9716222d318211/

I know it’s pretty custom configuration, but I’d like to have the ability to run Magento on a custom port without compromising reverse proxy.

Any idea what could go wrong here?