1

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
J21042
  • 1,214
  • 4
  • 19
  • 33
  • You don't need setup proxy for MongoDB. If you setup mongodb Uri for b-service like this: `mongodb://User:password@a-service-host:27017/database` should work (make sure your mongodb listen `0.0.0.0` – Valijon Mar 09 '20 at 17:58
  • Thank you Valijon for your comment, unfortunately that did not work for me. The mongodb is not running on a-service-host, and nginx needs to be configured to forward traffic to the 27017 port to the remote mongodb host that is on another network outside of the one in the docker sawrm. – J21042 Mar 12 '20 at 16:34

1 Answers1

0

You shold remove

https://

from your proxy_pass. Mongo is waiting for a TCP connection, not http.