美文网首页学习空间
Nginx的TCP和UDP反向代理

Nginx的TCP和UDP反向代理

作者: LoWang | 来源:发表于2019-07-15 09:46 被阅读0次

    Nginx的Stream模块支持TCP和UDP转发

    nginx从1.9.0版本开始,新增了ngx_stream_core_module模块,使nginx支持四层负载均衡。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream,使其支持stream代理。

    Nginx编译命令

    如果只需要支持TCP和UDP,那么可以直接最精简

    ./configure --prefix= --with-stream --without-http

    Nginx配置

    官方示例

    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;
        }
    }
    

    zk负载

    stream {
        upstream zk_server {
            server 172.16.3.8:2181 weight=5;
        }
        server {
            listen 2181 tcp;
            proxy_responses 1;
            proxy_timeout 20s;
            proxy_pass zk_server;
        }
    }
    

    Mysql负载

    user  nobody;
    worker_processes  auto;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        use epoll;
        worker_connections  1024;
    }
    
    stream {
        server {
            listen 3000;
            proxy_pass 127.0.0.1:3306;
    
        # support unix socket
        # proxy_pass unix:/var/lib/mysql/mysql.socket;
        }
    }
    

    UDP负载

    issues:

    DNS负载

    user  nobody;
    worker_processes  auto;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    events {
        use epoll;
        worker_connections  1024;
    }
    
    stream {
        server {
            listen 53 udp;
            proxy_pass 127.0.0.1:53;
    
        }
    }
    

    相关文章

      网友评论

        本文标题:Nginx的TCP和UDP反向代理

        本文链接:https://www.haomeiwen.com/subject/eusgkctx.html