区分udp数据包的源地址

时间:2018-07-18 19:48:18

标签: nginx configuration nginx-reverse-proxy

我目前有一个nginx实例,该实例在端口8000上侦听udp流量,然后将数据包发送到在端口5678上侦听的服务器。我被要求重新配置我的nginx,具体取决于谁(发送者的IP和端口)将数据包发送到端口8000,该数据包将被发送到相应的服务器。例如,如果进程一在localhost:1234上并将udp发送到端口8000,nginx会将其转发到服务器一。如果进程2在localhost:2345上将udp发送到端口8000,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;
        }


}

1 个答案:

答案 0 :(得分:0)

设法通过使用地图来解决。更新的配置现在是

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;
        }

}