#StackBounty: #docker #nginx #reverse-proxy #nginx-reverse-proxy #x-forwarded-for How to configure nginx X-Forwarded-Port to be the ori…

Bounty: 50

I am using nginx in a standard reverse proxy scenario, to pass all requests to /auth to another host, however I’m trying to use non-standard ports.

My end goal is to have the X-Forwarded-Port header set to the port that the request comes in on.

Here is my location block in nginx.conf:

location /auth/ {
    proxy_pass       http://otherhost:8090;
    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-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Port <VAR>;

This nginx is running in a docker container, that is configured to forward requests from 8085 into 80 in the container, such that the nginx process is listening on 80:>80/tcp

When I hit the URL:


I am correctly redirected to http://otherhost:8090, but the X-Forwarded-Port header is missing or wrong.

Where I have <VAR> in the original block, I have tried the following:

  • $server_port – This is the port nginx is listening on (80), not the
    request port.

  • $pass_port – Seems to be null in my setup, so nginx drops the

  • $http_port – This is a random port per request.

  • $remote_port – This is a random port per request.

I can change my config at deploy time to hardcode to the known port of incoming requests, but ideally I would be able to change the front port without any change to the nginx config.

I’ve scoured the nginx variable list but can’t find anything like $request_port. Is there any way for me to achieve my intent?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.