When running a Node application on your NGINX server you’re going to want to run it through a reverse-proxy for security reasons.

I use Forge to manage a few of my servers. The way I make this change is to view the site’s main info page and choose to edit the NGINX config.


server {
...

  //Comment out this code
  #location / {
  #    try_files $uri $uri/ /index.php?$query_string;
  #}

  location / {
      proxy_pass http://$YOUR_IP:$APP_PORT;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_cache_bypass $http_upgrade;
  }

..
}

You’ll need to replace the $YOUR_IP and $APP_PORT variables with the IP Address of your server and port you’re running your application on.

This will work just the same on NGNIX servers that are not associated with Forge. Just make sure to place it in the server block.

Now just make sure to restart NGINX

sudo service nginx restart

And that should do it – now you’ll be serving your application safely on your server. For a more detailed walkthrough, check out this walkthrough from Digital Ocean.