ScaleScaleScaleScale

Tips / Nginx


How to setup Nginx as Proxy for NodeJS

In some cases we need to setup NodeJS to work with Nginx as a reverse proxy. This is the way we can setup Nginx as proxy for NodeJS server, that usually listen in port 3000. If you have your nodejs runnin in a different port, just change the port number, it should work anyway.

http section inside Nginx configuration:

upstream app_nodejsĀ {
server 127.0.0.1:3000;
}

server block inside Nginx configuration:

location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_pass http://app_nodejs;
proxy_redirect off;
}

Restart Nginx:

service nginx restart

All done! With this setup we have a nginx proxy that will redirect all nodejs requests on port 3000 to nginx port running at 80 port. On the next post, we will cover how to ensure our nodejs will be always alive.

Popular search terms:

  • configure nginx to redirect all the /api/ calles to nodejs container
  • how to configure nginx with ssl on node
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1820 389KsTJhvBMNeuLrjnnV5minbxIR3wIv8r3hniilTOhjdKONvACCKrxPSuvnaUME b4ad15303c2a9b82fca29aceed0655a34e83ac33&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
  • https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1821 zRj8xm8SelTCq--fo3mwdjObxGV_YOPkT68mEfoq8vwSkFrQBwfbB7hrnq2_DRX6 0acacc3898a05c9b62483a9aecb39967409e0632&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Darryl Snow

    So how do you ensure your nodejs will always be alive?

    • Erkki

      use forever, it restarts whole thing when node crashes