区分udp数据包的源地址

Differentiate between source address of udp packets

我目前有一个 nginx 实例,它在端口 8000 上侦听 udp 流量,然后将数据包发送到在端口 5678 上侦听的服务器。我被要求重新配置我的 nginx,以便根据谁(IP 和端口) sender) 将数据包发送到8000端口,数据包将被发送到相应的服务器。例如,如果进程一在 localhost:1234 上并向端口 8000 发送一个 udp,nginx 会将其转发给服务器一。如果进程二在 localhost:2345 上向端口 8000 发送一个 udp,nginx 会将该数据包转发到服务器 2。这可能吗?这是我的配置文件。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}


stream {
        server {
                listen  127.0.0.1:8000 udp;
                proxy_pass      127.0.0.1:5678;
        }


}

设法通过使用地图解决了它。更新配置现在

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}


stream {
        #define the various upstreams
        upstream app_1 {
                server 127.0.0.1:5678;
        }

        upstream app_2 {
                server 127.0.0.1:9000;
        }
        #map source port to upstream
        map $remote_port $backend_svr {
                1234 "app_1";
                1235 "app_2";
        }
        #all udp traffic received on 8000, depending on source it will be redirected
        server {
                listen 8000 udp;
                proxy_pass $backend_svr;
        }

}