Nginx configuration for connecting to external mongodb from docker swarm
NickName:J21042 Ask DateTime:2020-03-10T01:48:21

Nginx configuration for connecting to external mongodb from docker swarm

I have a docker swarm running 2 python flask services, each one running in a different region, say A, and B. Region A is also running Nginx, which is the main entrypoint to access the services. There is a MongoDB database running in region A, outside of the docker swarm. The python service running in region A can connect to the MongoDB running in region A, but service B cannot connect to the MongoDB from region B. My question is can I configure the Nginx to proxy service B to connect to the MongoDB?

The Nginx locations config for the services is:

location /a {
  proxy_pass https://a-service:5000/;  # running in region A
}

location /b {
  proxy_pass https://b-service:5001/;  # running in region B
}

Where a-service and b-service are the docker swarm container names.

I saw the posts regarding how to setup MongoDB behind Nginx, but my case is the reverse - to access external MongoDB from inside a docker swarm through an Nginx inside the swarm.

(How to setup MongoDB behind Nginx Reverse Proxy)

I understand that I need to have something like this in the nginx.conf:

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

    upstream mongodb_host{
        server https://5.150.225.25:27017;
    }
}

However the location of the mongodb host (https://5.150.225.25) is another vm outside of the docker swarm, not a local IP. This results in an error:

nginx: [emerg] invalid host in upstream "https://5.150.225.25:27017" in nginx.conf

Copyright Notice:Content Author:「J21042」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60605888/nginx-configuration-for-connecting-to-external-mongodb-from-docker-swarm

More about “Nginx configuration for connecting to external mongodb from docker swarm” related questions

Nginx configuration for connecting to external mongodb from docker swarm

I have a docker swarm running 2 python flask services, each one running in a different region, say A, and B. Region A is also running Nginx, which is the main entrypoint to access the services. The...

Show Detail

Connecting to MongoDB in Docker from external app

Is it possible to connect to a docker container running a MongoDB image from an external nodejs application running locally? I've tried connecting via localhost:27017. Here's the docker compose fil...

Show Detail

Docker swarm LetsEncrypt nginx container writing to /var/log

I have a docker swarm running across 4 raspberryPis (1 manager, 3 workers). I was a little surprised today when I was diagnosing a crash on the master node and discovered that the container processes

Show Detail

Docker Swarm - Network Overlay not connecting all containers

I currently have 3 EC2 instances setup with Docker Swarm and Consul. I have 3 simple node apps spread across all 3 instances, and then nginx for routing on my swarm master. Using the guide for Ove...

Show Detail

How to manage Docker Swarm service configuration

Currently when we update a Docker Swarm service, we just run the relevant command in each environment. So for example if we need to add a mount to an external folder, we just run "docker service --...

Show Detail

How do I make nginx wait for my upstream service to start up in a Docker Swarm?

I deploy an nginx proxy service and a rails app service into a docker swarm. The nginx depends on the app in my docker-compose file. My nginx.conf file directs traffic to my upstream app service (

Show Detail

Docker swarm nginx bad gateway

I have one docker swarm service running in my worker node, which is TOMCAT instance. The command is for running it is like below:- docker service create --with-registry-auth --replicas 1 --network

Show Detail

How to disable external access to an overlay network in a docker swarm

I'm trying to set up a swarm that has the following services: nginx as a reverse proxy (mostly to deal with virtual hosts and SSL, though that's not in the nginx.conf). a golang web app. redis for

Show Detail

Docker Swarm container cannot connect to its Docker host, connection times out

long story short: Container C on Docker Swarm host A can access Nginx (deploy mode:global) on Docker Swarm host B but not on Docker Swarm host A via Docker host's IP, connection timed out. Long sto...

Show Detail

How to monitor nginx running in a docker swarm with prometheus?

Problem: Monitor Nginx Scenario: I have a Docker swarm with 4 replicas of Nginx. I need some basic metrics such as latency, HTTP status, etc. I checked out knyar/nginx-lua-prometheus. It's working ...

Show Detail