Nginx Proxy, Node.js, & Express

posted in: Uncategorized | 0

For those of you launching your own web servers, Nginx is a popular upgrade to using Apache, a web server that handles http requests. Nginx has more flexibility in the types of backend it works with such as Node.js, Ruby, and Python.

I’m going to explain how to set up Nginx as a proxy to work with Node.js and Express. This was done using:

  • Ubuntu Utopic 14.10
  • Node.js v0.10.x
  • Express 4.x (middleware web framework)
  • Nginx (latest release version)

 

Nginx

In the Nginx configuration (nginx.conf) inside the server {…} block:

If you have not noticed, I chose not to use localhost. Nginx will attempt to connect to ipv6 ::1 not to ipv4  127.0.0.1 when referring to localhost . Nginx allows  127.0.0.1 to listen to ipv4, ::1 for ipv6, and localhost to listen to both ipv6 and ipv4. In my configuration, Nginx will handle the outside communication on both ipv4 and ipv6, but for the Node.js process running behind Nginx it’s fine to listen only on ipv4.

References

Module ngx_http_proxy_module
localhost considered harmful (nginx ipv6 fun)

Node.js

In Express the 'trust proxy' is disabled by default, this has to be enabled if Express is behind a front facing proxy. As shown previously, above.

References

Express API settings