'how to enable stream module in nginx container

I want to proxy mongodb behind nginx. And I came across following code for same purpose.

My question is, how can I enable "stream" module in 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;
    }
}


Solution 1:[1]

Technically recompiling using the --with-stream option is required, as per the nginx docs. Fortunately there are plenty of existing images for that purpose. I've used this one personally: https://hub.docker.com/r/tekn0ir/nginx-stream

If using compose, your docker-compose.yml file would look something like this -

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

Create 1 or more files in the "/usr/local/nginx/conf/stream" directory containing your code stream { ... } . Config file names just need to end with the ".conf" extension. Then you can create any http config files in "/usr/local/nginx/conf/http".

Solution 2:[2]

Straight from the nginx documentation:

"The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter."

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Renni