, free & fully virtual. June 9th - 10th. Register Now

MongoDB reverse proxy and replica set

Hello guys,
I hope everyone is ok. I am sorry to bother you with some basic questions but I am new to mongodb and i am struggling a bit.

I am a junior devOps, and i have a practice project which consist in:
1 aws instance running a nodejs application (just a simple home page) running on port 3000
2 a mongodb (configured with the application and should connect on port 27017

Inside the app configuration, there is a variable called DB_HOST(where, from the instance, we should import the link


and we should be able to connect from the app instance IP:27017/posts to the mongodb and display some random blog posts.

Now, the process i followed, was using chef,packer and terraform to spin up aws instances. the app application works just fine, but if i try to access the mongodb, keeps loading the page but nothing happens.

Here are the steps i did.

Withthe application, i implemented a nginx reverse proxy, so the communication get through port 80 instead of port 3000

in mongodb i did this


# mongod.conf

# for documentation of all options, see:

# Where and how to store data.
  dbPath: /var/lib/mongodb
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
  port: <%= @port %>
  bindIp: <%= @bind_ip %>






## Enterprise-Only Options:



and in the attributre i set:

default[‘mongodb’][‘port’] = 27017
default[‘mongodb’][‘bind_ip’] = ‘’

doing this i should be able to connect from any ip to the port 27017.

Now, doing this, if i ssh inside my app instance and node seed my db, i can do so, but as soon as i run the command npm start, i get an error that there is another service running on port 3000.

plus i have another problem, as i need to implement a replicaset, in my DB_HOST i should implemente 3 different mongodb, so i did so:

export DB_HOST=mongodb://,,

but doing this, i get an error that i cant pass multiple values.

Any idea guys to sort out this? thank you very much for your help.

the format is mongodb://host:port,host:port,host:port/database?replicaSet=setName

The monouri format is documented here.

Hi chris, thank you very much for the time to reply.

i tried your code

export DB_HOST=mongodb://,,


(node:2579) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(node:2579) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:2579) UnhandledPromiseRejectionWarning: MongoError: no primary found in replicaset or invalid replica set name
    at /home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/replset.js:616:11
    at Server.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/replset.js:338:9)
    at Object.onceWrapper (events.js:315:30)
    at emitOne (events.js:116:13)
    at Server.emit (events.js:211:7)
    at Pool.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/topologies/server.js:377:12)
    at emitTwo (events.js:126:13)
    at Pool.emit (events.js:214:7)
    at connect (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/pool.js:624:10)
    at callback (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:109:5)
    at runCommand (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:172:5)
    at Connection.messageHandler (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connect.js:334:5)
    at emitTwo (events.js:126:13)
    at Connection.emit (events.js:214:7)
    at processMessage (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at Socket.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongoose/node_modules/mongodb/lib/core/connection/connection.js:533:15)
(node:2579) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:2579) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

this is my configuration

bash script for the app to seed the mongodb DB_HOST:


export DB_HOST=mongodb://,,

cd /home/ubuntu/AppFolder/app
node seeds/seed.js

sudo npm install
sudo npm start &

Primary and other replicasets DB_HOST:


echo '' >> /etc/hosts

sudo systemctl enable mongod
sudo systemctl start mongod

mongo mongodb:// --eval "rs.initiate( { _id : 'rs0', members: [{ _id: 0, host: '' }]})"
mongo mongodb:// --eval "rs.add( '' )"
mongo mongodb:// --eval "rs.add( '' )"
mongo mongodb:// --eval "db.isMaster().primary"
mongo mongodb:// --eval "rs.slaveOk()"

sleep 60; sudo systemctl restart metricbeat
sudo systemctl restart filebeat

sleep 180; sudo filebeat setup -e \
  -E output.logstash.enabled=false \
  -E output.elasticsearch.hosts=[''] \
  -E && sudo metricbeat setup

i realised that when my instance is up, it does execute all the command but it does not export the enviroment variable, so i need to do it manually.