Node.js apps — Express, Next.js, NestJS, Nuxt, a Telegram or Discord bot with a web dashboard — listen on a port such as 3000. In production you do not expose that port directly. Instead, Nginx sits in front as a reverse proxy: it terminates HTTPS, serves static files, handles slow clients and compression, and forwards requests to Node. A process manager keeps the app running and restarts it after crashes or reboots.
Architecture
Visitor ──HTTPS──▶ Nginx :443 ──HTTP──▶ Node.js app 127.0.0.1:3000
│
└─ serves /static files directly
The app listens only on 127.0.0.1, so it is unreachable from the internet except through Nginx.
Step 1: Install Node.js LTS
Distribution packages are often old. Use the NodeSource repository or a version manager. For a server, NodeSource is straightforward:
curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install -y nodejs
node -v && npm -v
(Replace 22 with the current LTS major version. On AlmaLinux/Rocky use https://rpm.nodesource.com/setup_22.x and dnf.)
Use an even-numbered LTS release for production; odd-numbered versions are short-lived.
Step 2: Deploy the app
Run the app as its own user, not root:
sudo adduser --disabled-password --gecos "" app
sudo mkdir -p /srv/app && sudo chown app:app /srv/app
sudo -iu app
cd /srv/app
git clone https://github.com/you/your-app.git current
cd current
npm ci --omit=dev
npm run build # if your app has a build step
Make the app listen on localhost and read the port from the environment, e.g. in Express:
const port = process.env.PORT || 3000;
app.listen(port, '127.0.0.1', () => console.log(`listening on ${port}`));
Test it: PORT=3000 node server.js and, from another terminal, curl http://127.0.0.1:3000.
Step 3: Keep it running
Option A: PM2
PM2 is a popular process manager for Node.js with clustering, logs and zero-downtime reloads.
sudo npm install -g pm2
sudo -iu app
cd /srv/app/current
pm2 start server.js --name api -i max --env production
pm2 save
exit
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u app --hp /home/app
-i max starts one process per CPU core (cluster mode) — only do this if your app is stateless or stores sessions in Redis/database. The pm2 startup command creates a systemd service so PM2 and your app start at boot.
Useful commands:
pm2 ls
pm2 logs api --lines 100
pm2 reload api # zero-downtime restart in cluster mode
pm2 monit
For repeatable setups, use an ecosystem.config.js file with environment variables and instance counts.
Option B: systemd
If you prefer no extra tools, a systemd unit works just as well for a single process — see running apps as systemd services.
Step 4: Nginx reverse proxy
Install Nginx (sudo apt install nginx) and create /etc/nginx/sites-available/app.example.com:
upstream node_app {
server 127.0.0.1:3000;
keepalive 32;
}
server {
listen 80;
server_name app.example.com;
# Serve built static assets directly (adjust the path to your app)
location /static/ {
alias /srv/app/current/public/static/;
expires 30d;
access_log off;
}
location / {
proxy_pass http://node_app;
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;
# WebSockets (Socket.IO, Next.js dev, live updates)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 60s;
}
}
The $connection_upgrade variable needs a map in the http block (for example in /etc/nginx/conf.d/upgrade.conf):
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
Enable and test:
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Tell Express it is behind a proxy
Otherwise req.ip is always 127.0.0.1 and secure cookies may not work:
app.set('trust proxy', 'loopback');
Step 5: HTTPS
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com --redirect
See Nginx and Let’s Encrypt for details. Then close port 3000 in the firewall if it was ever open — only 80 and 443 should be public.
Step 6: Zero-downtime deployments
A simple, reliable pattern is release directories with a symlink:
/srv/app/releases/20261119-1015/
/srv/app/releases/20261120-0930/
/srv/app/current -> releases/20261120-0930
Deploy script outline:
#!/usr/bin/env bash
set -euo pipefail
R=/srv/app/releases/$(date +%Y%m%d-%H%M)
git clone --depth 1 https://github.com/you/your-app.git "$R"
cd "$R" && npm ci --omit=dev && npm run build
ln -sfn "$R" /srv/app/current
pm2 reload api
ls -dt /srv/app/releases/* | tail -n +6 | xargs rm -rf # keep 5 releases
Rolling back is just pointing current at the previous release and reloading.
Performance tips
- Compression: let Nginx compress responses (see gzip and Brotli) and disable compression middleware in the app.
- Static files: serve from Nginx, not Express.
- Cluster mode: Node.js runs JavaScript on one core per process; PM2 cluster mode or multiple instances behind the upstream use all cores.
- Memory limits:
pm2 start server.js --max-memory-restart 500Mrestarts a process that leaks memory. - Rate limiting: protect login and API routes in Nginx — see Nginx rate limiting.
Troubleshooting
| Problem | Check |
|---|---|
| 502 Bad Gateway | Is the app running? pm2 ls; curl 127.0.0.1:3000 |
| WebSocket fails / falls back to polling | Upgrade and Connection headers, map block present |
req.ip is 127.0.0.1 |
trust proxy setting |
| App works locally, not after reboot | pm2 save and pm2 startup done? |
| 504 timeout on long requests | Increase proxy_read_timeout, or move work to a background job |