0
0
NginxHow-ToBeginner · 4 min read

How to Use Nginx with Node.js: Setup and Example

Use nginx as a reverse proxy to forward client requests to your Node.js server. Configure an nginx server block with proxy_pass pointing to your Node.js app's port, enabling better load handling and security.
📐

Syntax

The basic syntax to use nginx with Node.js involves setting up a server block that listens on port 80 or 443 and proxies requests to the Node.js app running on a different port, usually 3000.

Key parts:

  • server {}: Defines the server block.
  • listen 80;: Listens on HTTP port 80.
  • server_name: Your domain or IP.
  • location / {}: Defines URL path handling.
  • proxy_pass http://localhost:3000;: Forwards requests to Node.js app.
nginx
server {
    listen 80;
    server_name example.com;

    location / {
        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;
    }
}
💻

Example

This example shows a simple Node.js app running on port 3000 with nginx configured as a reverse proxy on port 80. Requests to http://localhost are forwarded to the Node.js server.

plaintext
/* Node.js app (app.js) */
const http = require('http');
const port = 3000;

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello from Node.js!');
});

server.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

/* Nginx config snippet (nginx.conf) */
server {
    listen 80;
    server_name localhost;

    location / {
        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;
    }
}
Output
Server running at http://localhost:3000/ When accessing http://localhost in a browser, the page shows: Hello from Node.js!
⚠️

Common Pitfalls

Common mistakes when using nginx with Node.js include:

  • Not setting proxy_set_header Host $host;, which can cause issues with hostname-based routing.
  • Forgetting to allow WebSocket headers (Upgrade and Connection) if your app uses WebSockets.
  • Running Node.js on a port blocked by firewall or not matching the proxy_pass port.
  • Not restarting nginx after config changes.
nginx
/* Wrong: Missing headers for WebSocket support */
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
    }
}

/* Right: Include headers for WebSocket and host forwarding */
server {
    listen 80;
    server_name example.com;

    location / {
        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;
    }
}
📊

Quick Reference

Tips for using nginx with Node.js:

  • Always use proxy_pass to forward requests to your Node.js app port.
  • Set headers Upgrade, Connection, and Host for WebSocket and proper routing.
  • Restart nginx after config changes with sudo systemctl restart nginx.
  • Use nginx -t to test config syntax before restarting.

Key Takeaways

Use nginx as a reverse proxy to forward requests to your Node.js app port.
Include necessary headers for WebSocket support and hostname forwarding.
Always test nginx configuration with nginx -t before restarting.
Restart nginx after any configuration changes to apply them.
Ensure your Node.js app is running and accessible on the specified port.