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