×

nginx端口转发

hqy hqy 发表于2020-03-06 14:43:31 浏览2058 评论0

抢沙发发表评论

端口转发

nginx.conf添加如下配置,并使用nginx -s reload重载nginx使其生效,同时注意防火墙/安全组放行对应的端口。

stream {    #将12345端口转发到192.168.1.23的3306端口
    server {        listen 12345;        proxy_connect_timeout 5s;        proxy_timeout 20s;        proxy_pass 192.168.1.23:3306;
    }    #将udp 53端口转发到192.168.1.23 53端口
    server {        listen 53 udp reuseport;        proxy_timeout 20s;        proxy_pass 192.168.1.23:53;
    }    #ipv4转发到ipv6
    server {        listen 9135;        proxy_connect_timeout 10s;        proxy_timeout 30s;        proxy_pass [2607:fcd0:107:3cc::1]:9135;
    }
}
  • listen:后面填写源端口(也就是当前服务器端口),默认协议为TCP,可以指定为UDP协议

  • proxy_connect_timeout:连接超时时间

  • proxy_timeout:超时时间

  • proxy_pass:填写转发目标的IP及端口号

注意:nginx可以将IPV4的数据包转发到IPV6,IPV6的IP需要使用[]括起来。

总结

目前能实现端口转发的工具大致有:rinetd、SSH、iptables、nginx、haproxy,其中rinetd配置最为简单,但不支持UDP转发,并且该软件已经好几年未更新,如果您服务器上已经安装了nginx,不妨用nginx做端口转发。


 您阅读本篇文章共花了: 

打赏

本文链接:https://kinber.cn/post/1180.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

群贤毕至

访客