Skip to content

Instantly share code, notes, and snippets.

@bugb
Last active March 24, 2023 09:48
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugb/e08f4edd1d7ba11706469ccd6cce1dfd to your computer and use it in GitHub Desktop.
Save bugb/e08f4edd1d7ba11706469ccd6cce1dfd to your computer and use it in GitHub Desktop.
Mongo DB behind Nginx as reverse proxy, share your Mongo DB with your friends in LAN network

Mongo DB works with raw TCP instead of HTTP so we need create stream with Nginx.

Add the below code above http block in nginx config file (eg: /etc/nginx/nginx.conf for Ubuntu)

stream {
    server {
        listen  9999;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass    stream_mongo_backend;
    }

    upstream stream_mongo_backend {
        server localhost:27017;
    }
}

The file after modified look like this:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	....

}

stream {
    server {
        listen  9999;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass    stream_mongo_backend;
    }

    upstream stream_mongo_backend {
        server localhost:27017;
    }
}

http {
...
}
...


Here you can share your Mongo DB server to your friend with: yourip:9999

@cyantarek
Copy link

What about connecting to MongoDB Atlas with Username and Password?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment