$uri with original port number behind NAT/Docker?

Please use this template for troubleshooting questions.

My issue:

I’d like to setup an nginx server which redirects to a subpath if root is visited.

Under normal circumstances, something like

listen            80;
location = / {
  return 302 ${uri}blog/;
}

does the job. 

But not if I am running nginx inside a docker container ( latest nginx:alpine from docker.io ) with a rootless podman.

Since a rootless container can’t access port 80 on the host side, I’m using port translation

127.0.0.1:7777:80 , i.e. translate port 7777 on the host side to port 80 inside the container.

But if requesting

http://127.0.0.1:7777/

even with a correct

GET / HTTP/1.1
Host: 127.0.0.1:7777

I do receive

HTTP/1.1 302 Moved Temporarily
Location: http://127.0.0.1/blog/

where :7777 is missing.

return 302 http://$host/blog/;
doesn’t work better.

Unfortunately, nginx’s online documentation doesn’t tell about where and when that number is stripped. It just says that it takes $host from the request’s Host: entry, which isn’t correct.

Which variable would I have to use to keep that port :7777 as given in the request’s Host: entry?

regards

Hi @hadmut!

Where are you doing the port translation? If it’s being done at the podman level, NGINX is likely not seeing the request to port 7777 at all. Have you tried using the NGINX unprivileged image instead?

Hi Alessandro,

On 10/6/25 13:30, Alessandro Fael Garcia via NGINX Community Forum wrote:

Where are you doing the port translation? If it’s being done at the
podman level, NGINX is likely not seeing the request to port 7777 at
all. Have you tried using the NGINX unprivileged image instead?

Yes, of course, it is the podman proxy (or, depending how you start it,
iptables/nftables), which translates port 7777 to port 80.

But that’s not the point.

Podman and iptables do translate the port on layer 4, *but they do not
change the contents of a TCP connection, especially not the HTTP
request. *So even if nginx running inside the docker image binds to port
80 and sees connections to port 80 on the TCP layer (layer 4 in the
layer model), The HTTP request still contains

Host: http://127.0.0.1:7777/

and the nginx docs claim, that the $host variable is take from the
request.So the question is: How can I use the port number given in the
Host: entry in the request for redirects?

BTW, Wordpress running in php behind nginx is correctly redirecting to
port 7777, so the information about port 7777 even goes through nginx.

regards

Hadmut

Thanks for the extra info! I assume you have tried using the http_host variable?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.