【问题标题】:Configure Nginx to be a TCP load balancer将 Nginx 配置为 TCP 负载均衡器
【发布时间】:2016-01-05 15:20:09
【问题描述】:

我想使用 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;
    }
}

你能告诉我如何正确配置它吗?

【问题讨论】:

    标签: nginx tcp load-balancing


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      brew install nginx-full --with-stream
      

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

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

      确保先安装水龙头。

      【讨论】:

      • 您可能还需要流 SSL 模块。我试图获得与 Alpine nginx docker 映像中使用的功能等效的构建,并且它们使用与预构建的官方 nginx 二进制文件相同的构建参数:
      • --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
      • 使用 brew 和 SSL 流安装,但没有运气:brew install homebrew/nginx/nginx-full --with-stream --with-stream_ssl_module。它似乎只是剥离了后者(以及任何未知参数)。我可以用nginx -V 验证这一点。事实上,在@DanielSmedegaardBuus 发布的列表中,只有--with-mail--with-stream 似乎有效。我通过在我的 linux VM 上安装它来解决它,但也许这里有人知道使用 brew 的解决方案?
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        ./configure --with-stream
        make
        make install
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2020-09-07
          • 2016-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多