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!