Search code examples
nginxtcpload-balancing

Configure Nginx to be a TCP load balancer


I want to use Nginx 1.9 to be a TCP load balancer. I followed the tutorial in https://www.nginx.com/resources/admin-guide/tcp-load-balancing/ but it didn't work.

Every time I tried to start nginx, I've got errors:

nginx: [emerg] unknown directive "stream" in /opt/nginx/nginx.conf

Here is my nginx.conf file:

events {
    worker_connections  1024;
}


http {
# blah blah blah
}

stream {
    upstream backend {
        server 127.0.0.1:9630;
        server 127.0.0.1:9631;
    }
    server {
        listen 2802;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass backend;
    }
}

Would you pls tell me how to configure it right?


Solution

  • The best way is compiling nginx from source to support stream directive:

    ./configure --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx  --conf-path=/opt/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-threads --with-stream --with-http_slice_module
    make
    sudo make install