将Nginx配置为TCP负载均衡器

时间:2016-01-05 15:20:10

标签: nginx tcp load-balancing

我想使用Nginx 1.9作为TCP负载均衡器。我按照https://www.nginx.com/resources/admin-guide/tcp-load-balancing/中的教程进行了操作,但它没有用。

每当我尝试启动nginx时,我都会遇到错误:

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

这是我的nginx.conf文件:

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

请问告诉我如何配置它?

4 个答案:

答案 0 :(得分:9)

最好的方法是从源代码编译nginx以支持stream指令:

./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

答案 1 :(得分:5)

在OS X上使用Homebrew,可以通过以下方式完成:

brew install nginx-full --with-stream

这可能会要求您先安装homebrew-nginx点按,在这种情况下您可能需要运行

brew install homebrew/nginx/nginx-full --with-stream

确保首先安装水龙头。

答案 2 :(得分:0)

如果您使用的是Linux,则有nginx standard repositories

答案 3 :(得分:0)

./configure --with-stream
make
make install
相关问题