nginx-tcp代理

Posted 79 months ago nginx tcp linux

编译的时侯加上 --with-stream
stream 
{
    upstream cloudsocket 
    {
    hash $remote_addr consistent;
    server 10.x.xx.14:1831 weight=5 max_fails=3 fail_timeout=30s;
    }
server 
    {
    listen 8081;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass cloudsocket;
    }
}




官方负载均衡的案例
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;
        proxy_responses 1;
        proxy_timeout 20s;
        proxy_pass dns;
    }

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

点击评论