0

I'm deploying a MERN app and wondering how the 'default' configuration file in etc/nginx/sites-available should be setup for the server, client, and Mongo database to work?

I am using a Ubuntu machine on Digital Ocean. I have the server.js running with Pm2 setup and working fine. Nginx and Mongo status checks come back fine. I am just having trouble with the configuration.

Here is one configuration of default I tried.

server {
    listen 80;
    server_name 138.197.173.139;
    root /var/www/MERN_App/client/build;

    error_page 404 /index.html;

    location / {
            add_header Cache-Control no-cache;
            try_files $uri $uri/ /index.html;
    }

    location /api/ {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Host $remote_addr;
            proxy_pass http://138.197.173.139:5002;
    }
}

Of course this doesn't show any data from the Mongo database because the address localhost:27017 is nowhere in this file. Though this gives the least errors and app flashes on screen with the above configuration, but in the console of course I get errors to the effect of 'cannot read ____ of undefined.'

The other thing I tried was 'stream'. I obviously don't know where this file even goes because it doesn't seem to belong in sites-available.

stream {
        upstream stream_mongo_backend {
                server 138.197.173.139:27017;
                }

        server {
                listen 80;
                server_name 138.197.173.139;
                root /var/www/MERN_App/client/build;

                error_page 404 /index.html;

                location / {
                        add_header Cache-Control no-cache;
                        try_files $uri $uri/ /index.html;
                }

                location /api/ {
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-Proto $scheme;
                        proxy_set_header X-Forwarded-For $remote_addr;
                        proxy_set_header X-Forwarded-Host $remote_addr;
                        proxy_pass http://138.197.173.139:5002;
                }
        }
}

This configuration will not allow me to restart nginx and the error in the log is 'stream can't go here'.
I don't know if there is some combination of a file in a new directory I have to create called 'stream' and a configuration in sites-available or something completely different.
If you have a good guide or video to setting up these files I would appreciate it because I find all of this opaque and I must be too thick for the Nginx docs.

Thank you for reading!

7
  • What is running at http://138.197.173.139:5002? – Tero Kilkanen Aug 5 '20 at 7:52
  • My server? That's the port it uses when I run it locally. – Renoldus Aug 5 '20 at 16:38
  • What does "My server" mean actually? – Tero Kilkanen Aug 5 '20 at 17:08
  • The back end. NodeJS. The thing with all of the routes. If a site is not static it has both a client and a server. – Renoldus Aug 5 '20 at 17:12
  • 1
    So, the overall request flow is like this: Client makes request to your nginx server. Nginx server passes the request to your NodeJS application. Your NodeJS then connects to the MongoDB server. This means that there doesn't need to be any MongoDB related things in nginx configuration. – Tero Kilkanen Aug 5 '20 at 17:37
0

You should not edit the default server block at all. You should instead create your own server block, in a separate configuration file in sites-available.

The default shipped with nginx is meant in part as an example, and in part to catch requests which are not directed to your web site's hostname.

Speaking of which, you should be using a hostname instead of an IP address to serve your web site, for a lengthy variety of reasons.

You are not streaming audio or video, so you should not be using stream at all.

From your description it sounds like your app is having problems. You should go back to debugging your application rather than doing things with nginx that at best will have no effect at all and at worst will break your site.

6
  • "You should not edit the default server block at all." There was no 'server' block until I created one. Do you mean the one called 'default' inside the sites-available dir? The app works just fine locally. – Renoldus Aug 5 '20 at 0:59
  • Yes, I mean the one shipped with nginx. – Michael Hampton Aug 5 '20 at 1:01
  • My problem is getting nginx to work with Mongodb. If it's not a stream what additional configuration file should handle Mongo? I don't need a host name yet. – Renoldus Aug 5 '20 at 1:07
  • @Renoldus Why would nginx be talking to MongoDB? – Michael Hampton Aug 5 '20 at 1:14
  • I don't know. You wrote, "You should instead create your own server block, in a separate configuration file in sites-available." Was this not related to Mongo? Has my OP violated some kind of rule? Because I'm having problems. I wrote what I tried. I posted what I tried. I find these configuration files completely opaque. Is there some guide or docs I can refer to so we don't have to keep going on like this? – Renoldus Aug 5 '20 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.