NGINX has created a complete Deployment Guide for using NGINX and NGINX Plus to load balance HTTP and HTTPS traffic across pools of Node.js® application servers.
Node.js is an open source, cross‑platform JavaScript runtime environment. It provides an agile‑savvy, module‑driven, scalable approach to the design, development, and deployment of applications.
This guide to Node.js load balancing applies to both on‑premises and cloud‑based Node.js deployments. It begins with the use of SSL/TLS certificates, basic load balancing for HTTP and HTTPS traffic, basic session persistence, content caching, and HTTP/2.
The guide goes on to describe enhanced Node.js load balancing with NGINX Plus using features including advanced session persistence, application health checks, live activity monitoring, and on‑the‑fly reconfiguration of upstream groups.
Best Practices for Node.js
The first step in load balancing Node.js application servers is to put a reverse proxy server in front of them running NGINX or NGINX Plus. Proxying the application servers offers many benefits, including:
- Protecting Node.js servers from direct exposure to Internet traffic
- Allowing flexibility in the use of multiple application servers
- Allowing implementation of SSL/TLS and HTTP/2
- Performance improvement by enabling caching at the proxy
- Enabling load balancing with both basic and advanced load balancing algorithms
However, load balancing Node.js is tricky – Node.js allows a high level of interaction between JavaScript code running in the web browser and JavaScript code running on the Node.js application server. To support this ongoing interaction, a given client session must run continually on a specific application server. This requires session persistence, which can be achieved with NGINX Open Source, but is achieved more easily with the session persistence features of NGINX Plus, as demonstrated in the Deployment Guide.
Node.js and the WebSocket protocol are natural partners, because WebSocket has a robust JavaScript interface. Socket.IO, a WebSocket API, is also frequently part of the mix. When transaction volumes are moderate, Node.js can also function as a web server. But, when transaction volumes rise, using NGINX Plus as a reverse proxy can help offload the application server by enabling caching and load balancing.
NGINX Plus also proxies the Socket.IO server, as described in Using NGINX and NGINX Plus with Node.js and Socket.IO, the WebSocket API. This prevents dependence on a single port – port 80 for HTTP, or port 443 for HTTPS – for the communications needs of a busy app. Instead, NGINX Plus can dispatch traffic to multiple application servers.
All this, and more, is demonstrated in our Deployment Guide. We hope you find this material useful as you work to improve the performance, reliability, and scale of your Node.js applications.
Additional Resources
This guide joins a number of other resources we’ve created for using Node.js and NGINX together:
- 5 Performance Tips for Node.js Applications, exploring ways to speed up Node.js using NGINX
- Using NGINX and NGINX Plus with Node.js and Socket.IO, the WebSocket API, explaining how to use WebSocket and Node.js with NGINX
- NGINX Plus Serves Breaking News for InkaBinka, a case study highlighting production use of Node.js
Special thanks to our friends at NodeSource, who helped us create this guide. NodeSource is the maker of N|Solid, a fully compatible Node.js runtime, enhanced to meet enterprise needs. NodeSource also offers support for both N|Solid and Node.js.