nginx反向代理tcp

nginx除了代理http协议外,从1.9.0版本开始新增了tcp协议的代理。

代理tcp协议准备工作

代理tcp需要使用stream模块(ngx_stream_core_module),默认configure不包含该模块,需要在configure的时候加上–with-stream

代理tcp的基本配置

官网示例

worker_processes auto;

error_log /var/log/nginx/error.log info;

events {
    worker_connections  1024;
}

stream {
    upstream backend {
        hash $remote_addr consistent;

        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    }

    upstream dns {
       server 192.168.0.1:53535;
       server dns.example.com:53;
    }

    server {
        listen 12345;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }

    server {
        listen 127.0.0.1:53 udp reuseport;
        proxy_timeout 20s;
        proxy_pass dns;
    }

    server {
        listen [::1]:12345;
        proxy_pass unix:/tmp/stream.socket;
    }
}

自己使用示例

stream {
    upstream cmp_zookeeper {
    hash $remote_addr consistent;
    server 10.9.71.11:22181 max_fails=3 fail_timeout=30s;
    }
    server {
        listen 2181;
        proxy_connect_timeout 10s;
        proxy_timeout 300s;
        proxy_pass cmp_zookeeper;
    }
}

若仅仅是需要反向代理,而不需要负载均衡的话。可以将这个示例简化一下。proxy_pass后边直接写ip和端口

stream {
    server {
        listen 3310;
        proxy_connect_timeout 10s;
        proxy_timeout 300s;
        proxy_pass 10.130.29.99:3306;
    }
}

官网地址:http://nginx.org/en/docs/stream/ngx_stream_core_module.html
中文地址:https://www.docs4dev.com/docs/zh/nginx/current/reference/stream-ngx_stream_core_module.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值