I want to proxy mongodb behind nginx. And I came across following code for same purpose.
stream {
server {
listen 27020;
proxy_connect_timeout 5s;
proxy_timeout 20s;
proxy_pass mongodb_host;
}
upstream mongodb_host{
server xx.xxx.xxx.xx:27017;
}
}
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With