Home > Net >  NGINX UDP Proxy listening same port and decides on origin ip
NGINX UDP Proxy listening same port and decides on origin ip

Time:05-14

I am currently trying to make a nginx proxy work where it pass to different ips depending on the origin.

stream {
    server {
        listen 1000 udp;
        proxy_pass 10.0.0.2;
        allow 10.0.0.3;
    }
    server {
        listen 1000 udp;
        proxy_pass 10.0.0.3;
        allow 10.0.0.2;
    }
}

obviously this does not work as I can not listen on the same port twice. I tried something with "if" but it is not allowed there. Any ideas? I just want to proxy the traffic between the two ips.

CodePudding user response:

You need transparent proxy or some kind of packet filter or firewall, not nginx, since it is reverse proxy and not suitable for your task.

CodePudding user response:

While I'm not sure you choose the right way to solve your task (unless you need some kind of load-balancing), however this this should be possible using several upstream blocks and the geo block:

stream {
    upstream first_upstream {
        server 10.0.0.2:1000;
    }
    upstream second_upstream {
        server 10.0.0.3:1000;
    }
    upstream third_upstream {
        server 10.0.0.4:1000;
    }
    geo $upstream_name {
        10.0.0.0/24  first_upstream;
        10.0.1.0/24  second_upstream;
        default      third_upstream;
    }
    server {
        listen 1000 udp;
        proxy_pass $upstream_name;
    }
}

If you need a load-balancing, see the TCP and UDP Load Balancing article.

  • Related