11

nginx tcp转发

 2 years ago
source link: http://blog.ilibrary.me/2021/11/27/nginx-tcp%E8%BD%AC%E5%8F%91
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
nginx tcp转发
欢迎转载,请支持原创,保留原文链接:blog.ilibrary.me

nginx支持tcp的转发,是通过stream模块转发,不是通过http模块转发。

  1. 不支持server_name, 也就是不支持通过不同的域名来把同一端口的连接转发给不同的服务.
  2. 可以通过upstream server来做负载均衡.
  3. 最新的nginx docker(1.21)是默认开启了--with-stream编译参数的,开箱即用, 可以通过nginx -V查看是否带有--with-stream
  4. 支持ssl证书. 具体配置可以参考文末链接。
  5. 配置都要放到nginx.conf,千万别放到sites-available这种地方,不然就会进入到http部分.
  1. 在nginx.conf末尾添加下面内容:
    	# tcp/ip proxy,
     # 加载tcp.d目录下面的配置,实现tcp/UDP端口转发,
     include /etc/nginx/tcp.d/*.conf;
    
  2. 添加tcp.conf的配置到/etc/nginx/tcp.d/目录:
     # https://blog.51cto.com/moerjinrong/2287680
     stream {
         upstream xy_tcp {
             hash $remote_addr consistent;
             server xy-tcp:55000 max_fails=3 fail_timeout=10s;
    
         }
         server {
             listen 55000;
    
             # "server_name" directive is not allowed here
             # server_name xxx.com;
             proxy_connect_timeout 20s;
             proxy_timeout 5m;
             proxy_pass xy_tcp;
         }
     }
    
扫描二维码分享到微信朋友圈Loading...Please wait
qrcode.php?url=%2F2021%2F11%2F27%2Fnginx-tcp%25E8%25BD%25AC%25E5%258F%2591

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK