将Nginx配置为TCP负载均衡器

7
我想使用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个回答

10

最好的方法是从源代码编译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

6

在 OS X 上使用 Homebrew,可以这样做:

brew install nginx-full --with-stream

这可能会要求您先安装homebrew-nginx tap,如果是这样,您可能需要运行以下命令:

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

确保先安装水龙头。


1
你可能也需要流SSL模块。我试图获取一个与Alpine nginx docker镜像中使用的功能等效的构建,并且它们使用与预构建的官方nginx二进制文件相同的构建参数: - DanielSmedegaardBuus
--with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6 - DanielSmedegaardBuus
使用brew和SSL流进行安装,但没有成功:brew install homebrew/nginx/nginx-full --with-stream --with-stream_ssl_module。它似乎只是剥离了后者(以及任何未知的参数)。我可以通过nginx -V来验证这一点。实际上,在@DanielSmedegaardBuus发布的列表中,只有--with-mail--with-stream似乎有效。我通过在我的Linux虚拟机上安装解决了这个问题,但也许这里有人知道使用brew的解决方案? - Johannes Hoff

0

0
./configure --with-stream
make
make install

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接