Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reboot a node.js production server

Let's say I have a nodejs/express application on production server. If I want to add a new router, I have to reboot one, right? But if I reboot my node.js server, then users could get an error.

So how should I reboot my node.js server without errors for my users?

like image 907
Erik Avatar asked Jan 17 '23 08:01

Erik


1 Answers

If you proxy to your Node.js app with Nginx, you can tell your Node app to listen on a socket and then only shut down the old server if the new one starts correctly; once the old server shuts down, Nginx will route requests to the new instance of the server. This is how Unicorn, a popular Ruby server, works.

In Nginx, you would specify your upstream like this:

upstream node_server {
  server unix:/path/to/socket.sock;
}

server {
  ...
  location / {
    ...
     proxy_pass http://node_server;
  }
}

And in node, you can listen on a socket with

server.listen('/path/to/socket.sock', function() {
  console.log("Listening on socket.");
});
like image 180
Michelle Tilley Avatar answered Jan 22 '23 05:01

Michelle Tilley