使用nginx作udp协议的反向代理

反向代理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!

问题1:如何通过nginx向udp后端服务传递客户真实IP

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值