Home > Net >  When changing from express to nginx
When changing from express to nginx

Time:02-11

I'm a node js newbie and I'm looking for various materials now

When changing from express to nginx

Does it matter if the backend code inside doesn't change?

Things like nginx or apache don't seem to have anything to do with the backend code, so I'm asking just in case.

  app.get(
    "/hi",
    function (req, res, next) {
      res.send("hi");
    },
  );

In the examples that did not use nginx, the code structure inside was almost like this. If you use nginx, nothing special will change in the backend code, right?

CodePudding user response:

You are describing a Reverse Proxy Use case. Short answer is in 90% of the cases NO - you don't have to change anything at your nodejs code.

A simple configuration could look like this:

Client -- req TCP 80 -> NGINX -- req TCP 3000 -> NODEJS EXPRESS | app.js

The configuration


upstream node_backend {
  server 127.0.0.1:3000;
  keepalive 32;
}

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://node_backend/;
    proxy_set_header Host $host;
    ...
  }
}

Notes:

  1. Make sure you are managing the NodeApp in a proper way. Means how to start and stop it on system startup or something like that.

  2. You would likely need HTTPS on the NGINX Side. This will not change the location configuration or the proxy_pass statement.

  • Related