How to configure nginx X-Forwarded-Port to be the originally request port How to configure nginx X-Forwarded-Port to be the originally request port docker docker

How to configure nginx X-Forwarded-Port to be the originally request port


The only workaround I've found is to use a map rule to get the port from the http_host variable e.g.

    map $http_host $port {      default 80;      "~^[^\:]+:(?<p>\d+)$" $p;    }


This is a just rough idea to write Nginx conf, but I am sure this can help you in redirection

server {        listen 80;      server_name host.docker.internal;       # By default land on localhost:80 to root so in root we copied UI build to the ngnix html dir.    # have a look to docker-compose uiapp service.    location / {                root   /usr/share/nginx/html;               index  index.html index.htm;        }      # after location add filter, from which every endpoint starts with or comes in endpoint    # so that ngnix can capture the URL and reroute it.   # like /backend/getUserInfo/<UserId>    # In above example /backend is that filter which will be captured by Ngnix and reroute the flow.    location /backend {         proxy_set_header X-Forwarded-Host $host;            proxy_set_header X-Forwarded-Server $host;          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        #proxy_pass http://<ContainerName>:<PortNumber>;         # In our case Container name is as we setup in docker-compose `beservice` and port 8080        proxy_pass http://beservice:8080;       }   }

For more details you can have a look at this project

https://github.com/dupinder/NgnixDockerizedDevEnv


Strange that this hasn't been answered yet, but the answer is

proxy_set_header X-Forwarded-Port $server_port;