Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 900
anhldbk Avatar asked Jan 05 '16 15:01

anhldbk


People also ask

Does NGINX support TCP load balancing?

Load balancing refers to efficiently distributing network traffic across multiple backend servers. In NGINX Plus Release 5 and later, NGINX Plus can proxy and load balance Transmission Control Protocol) (TCP) traffic.

Can NGINX be used as load balancer?

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

Does NGINX use TCP or UDP?

Along with HTTP traffic, NGINX Ingress Controller load balances TCP and UDP traffic, so you can use it to manage traffic for a wide range of apps and utilities based on those protocols, including: MySQL, LDAP, and MQTT – TCP‑based apps used by many popular applications.


1 Answers

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
like image 100
anhldbk Avatar answered Sep 30 '22 13:09

anhldbk