Node.js Course
Node.js
/
Advanced

Nginx Reverse Proxy

Definition

A high-performance web server that is heavily used as a Reverse Proxy and Load Balancer sitting in front of Node.js applications.

Explain Like I'm New

Node.js is fantastic at processing business logic, but it is terrible at serving static files (like huge images) or managing SSL/HTTPS certificates. NGINX is the massive steel gate at the front of your server. It catches all incoming internet traffic, handles the HTTPS security instantly, serves static images directly from the hard drive, and then silently passes the complex API requests back to your fragile Node.js app.

Real World Example

A user visits `https://yoursite.com/api/users`. NGINX receives the request on port 443 (HTTPS), decrypts it, and forwards it to your Node app hiding on `localhost:3000`. The Node app processes it, hands the data back to NGINX, and NGINX sends it to the user.

Common Use Cases

  • •Handling HTTPS/SSL certificates
  • •Serving static assets blazingly fast
  • •Load balancing across multiple PM2 instances

Terminal Output

bash / terminal
# --- TYPICAL NGINX CONFIGURATION --- # server { # listen 80; # server_name mywebsite.com; # # # 1. Serve static files directly (Extremely fast) # location /images/ { # root /var/www/mywebsite/public; # } # # # 2. Pass API traffic to the hidden Node.js server # location /api/ { # proxy_pass http://localhost:3000; # proxy_http_version 1.1; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection 'upgrade'; # proxy_set_header Host $host; # proxy_cache_bypass $http_upgrade; # } # } console.log("NGINX + PM2 is the classic, battle-tested way to deploy Node on a Virtual Private Server (VPS).");

Interview Questions

basic

  • What is a Reverse Proxy?

intermediate

  • Why not just use Node.js to serve HTTPS traffic directly?

Flash Cards

Question

What is a Reverse Proxy?

Click to reveal answer
Answer

A server that sits in front of backend servers and forwards client requests to those servers. The client never knows the backend server exists.

Question

Why not use Node for HTTPS?

Click to reveal answer
Answer

Because cryptography (SSL/TLS decryption) is heavily CPU intensive. If Node does it, it blocks the single-threaded Event Loop, causing massive slowdowns. NGINX is written in highly optimized C and can decrypt thousands of connections concurrently without breaking a sweat.