I am new to nginx and I know how to run simple root /var/www/example.com if it has html, it will run. But what if I have Angular app, and I have to run server.js in order to get ssr working ?
How to achieve something similar to this :
I suppose your intention is to deploy your app on a remote server, so this is what you need.
Run node server.js locally continuously. To do this you can use pm2 which is installed using yarn on npm. Make sure you install it globally:
npm install pm2 -g
then navigate to your root folder and run your app using
pm2 start server.js
Your app is now daemonized, monitored and kept alive forever.
Now we head to nginx. you will need to make edits to the config file by the name default in this location: /etc/nginx/sites-enabled
sudo nano /etc/nginx/sites-enabled/default
Now look for the closing tag of:
server {
} #this is its closing tag
Add a proxy before the closing tag to direct all traffic from port 80 to your app ...
say for example my server.js app is running on port 3000 locally, i will add this code so that the file looks like this:
server{
#a bunch of code that was preexisting.......
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
At least this would give some insights to what you are after.... hope it helps.