One upstream. One proxy_pass. And nginx sits in front of our Node.js app. In episode 4 we set up nginx, in episode 5 we built a Node.js app on port 3000. Today we connect them: nginx becomes a reverse proxy, visitors only ever talk to port 80, and port 3000 disappears from the internet.
- nginx on port 80 forwarding every request to the Node.js app on
127.0.0.1:3000 - A config where every line is explained
- A one-command check that proves the reverse proxy works
- Port 3000 closed again in the Lightsail firewall
Where we start
The Node.js app from episode 5 is running under pm2, and nginx still serves the static files from episode 4:
pm2 status
cat /etc/nginx/sites-available/fineshot
curl -sI http://localhost | grep -iE 'server|powered'
The headers show only Server: nginx. There is no Express header yet, because nginx answers on its own.
Rewrite the config as a reverse proxy
Open the site config (sudo vim /etc/nginx/sites-available/fineshot), delete the old lines and write the reverse proxy config:
# upstream = a named group of backend servers
# our Node.js app (managed by pm2) runs on port 3000 on this same server
upstream fineshot_node {
server 127.0.0.1:3000;
}
server {
# nginx receives all public HTTP traffic on port 80
listen 80;
# _ = accept requests for any domain name or IP address
server_name _;
location / {
# reverse proxy: forward every request to the upstream (Node.js)
proxy_pass http://fineshot_node;
proxy_http_version 1.1;
# pass the original domain/IP the visitor typed to Node.js
proxy_set_header Host $host;
# pass the real visitor IP (otherwise Node.js only sees 127.0.0.1)
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream fineshot_nodeis a named group of backend servers. Ours is one: the Node.js app on127.0.0.1port 3000.listen 80andserver_name _: nginx still receives all public HTTP traffic on port 80, for any domain or IP.proxy_pass http://fineshot_nodesends every request to that upstream.proxy_set_header Hostpasses on the domain the visitor typed, andX-Real-IPplusX-Forwarded-Forpass on the visitor’s real IP. Without them, Node.js would only ever see127.0.0.1.
Test and reload nginx
sudo nginx -t
sudo systemctl reload nginx
As always, test first. syntax is ok and test is successful mean it’s safe to reload, and a reload applies the new config without downtime.
Prove the reverse proxy works
curl -sI http://localhost | grep -iE 'server|powered'
Now you see two lines: Server: nginx answered the request, but X-Powered-By: Express built the page. That is a reverse proxy.
Open the site on port 80
Open http://YOUR_SERVER_IP on plain port 80, then /contact.html. The site and the Contact Us page load through nginx, and the visitor never sees port 3000. Want a domain name here instead of an IP? Episode 2 covers the domain and the free SSL certificate.
The contact form works through nginx
curl -s -X POST http://localhost/contact -d 'name=Proxy User&email=proxy@example.com&message=Sent through the nginx reverse proxy'; echo
tail -n 1 ~/fineshot-app/contacts.txt
We post to port 80, nginx passes the request to Node.js, and the newest line in contacts.txt is our message.
Close port 3000 in the Lightsail firewall
Port 3000 is no longer needed from outside. In Lightsail go to Networking → IPv4 firewall and delete the custom rule for port 3000. Now the only public door is nginx on port 80, and Node.js stays safely behind it.
If it doesn’t work
502 Bad Gateway. nginx can’t reach Node.js. Check the app with pm2 status and curl -s http://127.0.0.1:3000 | head -3, and make sure the upstream says port 3000.
nginx -t fails. Usually a missing ; or }. The error names the file and line number. Fix it before you reload: a failed test means the running config is untouched.
No X-Powered-By: Express header. The old static config is still loaded. Check that /etc/nginx/sites-enabled/ links to the edited file, then reload.
Next
nginx is in front, Node.js is safely behind it, and port 3000 is closed. To serve this on your own domain with HTTPS, go back to episode 2: the domain and the free SSL certificate fit right on top of this setup.
