Skip to content
Log In Sign Up info@muvcloud.com

The Complete Configuration

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

Save it as /etc/nginx/sites-available/app.example.com and link it into sites-enabled on Debian and Ubuntu, or as /etc/nginx/conf.d/app.example.com.conf on AlmaLinux and Rocky Linux. These files are read inside the http context, where the map block belongs. Then run sudo nginx -t and sudo systemctl reload nginx; a failed test leaves the running server untouched. Setting up the file itself is covered in Nginx server blocks. Once the proxy answers over HTTP, add HTTPS with Certbot for Nginx.

What the Headers Do

  • Host: passes the host name the visitor asked for. Without it the application receives the address from proxy_pass.
  • X-Real-IP: the client's address. Every request otherwise seems to come from 127.0.0.1.
  • X-Forwarded-For: the chain of client and proxy addresses, with this hop appended.
  • X-Forwarded-Proto: http or https, so the application builds correct links and does not redirect in a loop.

Tell the application to trust these headers only from 127.0.0.1. In Express, for example, that is app.set('trust proxy', 'loopback').

WebSocket Connections

A WebSocket starts as an HTTP request with an Upgrade header. Both are hop-by-hop headers, so Nginx does not pass them to the backend by itself. Before version 1.29.7 it also speaks HTTP/1.0 to backends by default, and the Nginx packages in current Ubuntu, Debian, AlmaLinux, and Rocky Linux releases are older than that. The proxy_http_version 1.1 line and the two upgrade headers fix both; the map sends "close" when a request asks for no upgrade. A WebSocket that carries no data is closed after proxy_read_timeout, 60 seconds by default, so let the application send ping frames, or raise the timeout in that location.

Keep the Application on 127.0.0.1

const http = require('http');
http.createServer((req, res) => {
  res.end('Hello from the app\n');
}).listen(3000, '127.0.0.1');

An application bound to 127.0.0.1 can only be reached from the server itself, so every visitor goes through Nginx. Bound to 0.0.0.0, port 3000 is open to the internet unless a firewall blocks it. sudo ss -tlnp | grep 3000 shows which address it uses. Background is in what is localhost, and Node.js setup in how to install Node.js on Ubuntu. Run the application as a service so it starts at boot; see systemd unit files.

The Trailing Slash in proxy_pass

location /app/ {
    proxy_pass http://127.0.0.1:3000/;
}

Without a path after the port, Nginx passes the request path unchanged: /app/users arrives as /app/users. With a path, even a single slash, the part that matched the location is replaced: /app/users arrives as /users. Pick the form that matches what the application expects.

Timeouts and Buffering

  • proxy_connect_timeout: how long Nginx waits to open the connection; 60 seconds by default, and it usually cannot exceed 75 seconds.
  • proxy_read_timeout: 60 seconds by default, measured between two successive reads, not over the whole response. Raise it only in the location that needs it; see 504 Gateway Timeout in Nginx.
  • proxy_buffering: on by default, so Nginx collects the response and a slow client does not hold the application busy. Turn it off for streamed responses such as server-sent events.

Several Backends with upstream

upstream app_backend {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
}

Put the block in the http context and write proxy_pass http://app_backend;. Nginx spreads requests in turn (round-robin). After a failed attempt it skips that server for 10 seconds by default, set with max_fails and fail_timeout.

SELinux on AlmaLinux and Rocky Linux

sudo setsebool -P httpd_can_network_connect 1

Without this setting SELinux stops Nginx from connecting to the port, and visitors get a 502. Do not open port 3000 in the firewall and do not disable SELinux. Other causes of that error are in 502 Bad Gateway in Nginx.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations