反向代理udp
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
- 注意:stream:server模块中不支持tcp协议中的location配置二级目录转发
确定nginx版本及模块【注意】
nginx -V
- 版本号需要>1.9.0
- 模块有 --with-stream【注意检查】
增加配置
因为udp与tcp是同级的两种通信协议,所以与http模块平齐
- nginx.conf文件内容增加如下stream配置
# udp模块,使用stream关键字,15433代理5433
stream {
upstream udptest {
server 192.168.1.33:5433;
}
upstream udptest2 {
server 192.168.1.33:5434;
}
server {
listen 15433 udp reuseport ;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass udptest;
}
server {
listen 25434 udp reuseport ;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass udptest2 ;
}
#ipv4转发到ipv6
server {
listen 19135;
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass [2607:fcd0:107:3cc::1]:19135;
}
}
# http 协议模块 ....
udp :说明是代理udp协议
reuseport :处理多个包为同一个会话时使用,保持会话的作用
测试
- 通过nc开启源udp服务端
nc -l -u 192.168.1.33 5433
- 通过另一个ssh容器连接代理udp端口
nc -uvz 192.168.1.33 15433
# Connection to 192.168.1.33 15433 port [udp/*] succeeded!