首页 > 解决方案 > 如何在 nginx 容器中启用流模块

问题描述

我想在 nginx 后面代理 mongodb。我出于同样的目的遇到了以下代码。

我的问题是,如何在 nginx 中启用“流”模块?

stream {
    server {
        listen 27020;
        proxy_connect_timeout 5s;
        proxy_timeout 20s;
        proxy_pass    mongodb_host;
    }

    upstream mongodb_host{
        server xx.xxx.xxx.xx:27017;
    }
}

标签: mongodbnginxdevopsnginx-reverse-proxy

解决方案


根据 nginx 文档,在技术上需要使用 --with-stream 选项重新编译。幸运的是,有很多现有的图像可以用于此目的。我个人使用过这个:https ://hub.docker.com/r/tekn0ir/nginx-stream

如果使用 compose,您的 docker-compose.yml 文件将如下所示 -

version: '3'  
services:  
    service1:  
        ...  
    service2:  
        ...  
    nginx:  
        image: tekn0ir/nginx-stream:latest  
        ports:   
            - "27020:27020"   
        volumes:   
            - /usr/local/nginx/conf/http:/opt/nginx/http.conf.d
            - /usr/local/nginx/conf/stream:/opt/nginx/stream.conf.d

在包含您的代码的“/usr/local/nginx/conf/stream”目录中创建 1 个或多个文件stream { ... } 。配置文件名只需要以“.conf”扩展名结尾。然后你可以在“/usr/local/nginx/conf/http”中创建任何http配置文件。


推荐阅读