I have a nodejs app which uses mongodb for persistance. I am going to deploy it on two vms and I am going to use nginx to configure the loadbalancing setup. Now, how the mongodb should be installed? I understand that I should install it in any one of the vm. That can refer it just as localhost. How the mongodb should be made available to load balancing server(The other server)?
Based on what I see as your desired setup: nginx load balancer, two app instances, and mongodb, I would highly recommend four servers.
Server 1: nginx load balancer. The main entry point for your cluster. This is the server your public domain points to.
Servers 2, 3: Node.js application instances. nginx is configured to load balance between these two servers. As your application grows, you can continue to add nodes at this layer to keep up with demand.
Server 4: mongodb. Your Node.js instances can all be configured to point to this mongo instance. At a minimum you should probably have a fifth server for a mongo secondary, but that's up to you.