My Node.js app runs on port 4000 on an AWS EC2 server. Nginx is installed on the same Ubuntu server. But visitors still have to type :4000 at the end of the URL, because Nginx doesn't know about the app yet.
In this guide I set up Nginx as a reverse proxy for Node.js. Nginx listens on port 80 and passes every request to the app on port 4000. When you're done, your app opens on the plain IP, with no port in the URL.
It takes one config file and five commands. I show each one below, with what it does and what to do when it fails.
Quick answer: the commands
If you just need the steps, here they are. Each one is explained below.
cd /etc/nginx/sites-available sudo vi node-api # paste the server block from Step 2 sudo nginx -t sudo ln -s /etc/nginx/sites-available/node-api /etc/nginx/sites-enabled/ sudo systemctl restart nginx What you need first
- An Ubuntu server (I use AWS EC2) that you can SSH into
- Nginx installed and running (how I installed it)
- A Node.js app running on a port, like 4000 (I keep mine alive with PM2)
- Port 80 open in your EC2 Security Group
What is an Nginx reverse proxy?
A reverse proxy sits in front of your app and talks to the visitor for it. Here's the flow:
- A browser sends a request to your server's IP on port 80.
- Nginx gets it and forwards it to
http://127.0.0.1:4000. - Your Node app handles it and sends the response back to Nginx.
- Nginx sends that response to the browser.
The visitor only ever talks to Nginx. Port 4000 stays hidden. Later you can add a domain and HTTPS in the same Nginx file, without touching your Node code.
Step 1: Create the Nginx config file for your Node app
Nginx keeps site configs in /etc/nginx/sites-available. There's already a file called default there. I add my own, called node-api:
cd /etc/nginx/sites-available sudo vi node-api Vim opens in normal mode, so typing does nothing yet. Press i to switch to insert mode. You'll see -- INSERT -- at the bottom. (You can use sudo nano node-api instead if you prefer nano.)
Step 2: Add the reverse proxy server block
Paste this in:
server { listen 80; server_name YOUR_EC2_PUBLIC_IP; location / { proxy_pass http://127.0.0.1:4000; 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; } } Change two things:
- Replace
YOUR_EC2_PUBLIC_IPwith your instance's public IP. It's on the instance details page in the EC2 console. If you have a domain, put the domain here. - Replace
4000with the port your app listens on.
What each line in the config does
-
listen 80;Nginx listens on port 80, the normal HTTP port. It's the default, but writing it makes the file clear. -
server_nameThe domain or IP people use to reach your app. I don't have a domain yet, so I use the public IP. -
location /Rules for every path. All URLs start with/, so this catches every request. -
proxy_pass http://127.0.0.1:4000;The line that makes it a reverse proxy. It sends the request to your Node app. -
proxy_http_version 1.1;Talk to Node over HTTP 1.1. WebSockets and keep-alive connections need it. -
proxy_set_header UpgradeandConnection 'upgrade'Let a normal HTTP connection switch to a WebSocket, for things like Socket.IO. -
proxy_set_header Host $host;Passes the original host name to Node, so your app knows which domain was asked for. -
proxy_cache_bypass $http_upgrade;Skips the cache for WebSocket connections, so live data isn't cached.
If your app doesn't use WebSockets, keep those lines anyway. They don't hurt, and they save you a confusing bug later.
Step 3: Save the file and quit Vim
Press Esc to leave insert mode. Then type this and press Enter:
:wq w writes the file and q quits.
Step 4: Test the Nginx config with nginx -t
Always test before you restart:
sudo nginx -t You want to see these two lines:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful If it fails, the error gives you the file and the line number. A missing ; at the end of a line is the most common cause.
Step 5: Enable the site (sites-available vs sites-enabled)
Nginx doesn't load files from sites-available. It only loads what's in sites-enabled. Think of the first folder as a storage room and the second as the active room.
So link your file into sites-enabled:
sudo ln -s /etc/nginx/sites-available/node-api /etc/nginx/sites-enabled/ ln -s makes a symbolic link, a shortcut instead of a copy. When you edit the file in sites-available later, the change shows up in sites-enabled too.
Check that the link is there:
ls -l /etc/nginx/sites-enabled This setup is handy. To turn a site off, delete the link and keep the file.
Step 6: Restart Nginx
The new config does nothing until Nginx loads it:
sudo systemctl restart nginx Now open http://YOUR_EC2_PUBLIC_IP in the browser. Your Node app should load with no :4000.
Nginx reverse proxy not working? Common errors
-
nginx -tfails: read the line number in the error. Look for a missing;or}. - You still see "Welcome to nginx!": the
defaultsite is catching the request. Check thatserver_nameis your exact public IP, or remove the default site withsudo rm /etc/nginx/sites-enabled/defaultand restart Nginx. - 502 Bad Gateway: Nginx is fine, but nothing answers on port 4000. Check your app with
pm2 statusorcurl http://127.0.0.1:4000. - "This site can't be reached": port 80 isn't open. Add an HTTP rule (port 80) to your EC2 Security Group.
FAQ
Should I use restart or reload?
Both work. sudo systemctl reload nginx loads the new config without dropping open connections, so it's nicer on a live site. restart stops and starts Nginx fully.
Do I still need PM2 if I use Nginx?
Yes. Nginx only forwards requests. PM2 keeps your Node app running and restarts it if it crashes or the server reboots.
Does this work for Express, NestJS or Next.js?
Yes. Nginx doesn't care what framework you use. If the app listens on a port, proxy_pass can point at it.
Why 127.0.0.1 and not the public IP?
Nginx and Node run on the same server, so Nginx talks to the app locally. That's faster, and you never have to open port 4000 to the internet.
Can I add a domain and HTTPS later?
Yes. Put your domain in server_name, point the domain's DNS to the server, and use Certbot to add a free SSL certificate. The location block stays the same.
What's next
In the next lesson I open the server's IP in the browser and test that the reverse proxy works.
Did sudo nginx -t pass on your first try? Tell me in the comments. If you got an error, paste it and I'll help you fix it.
This post is part of my free series on deploying a Node.js app to AWS from zero. Full course playlist: https://www.youtube.com/playlist?list=PLfeDvEEUyoEJZGnw11rKIET5lJDs6JmhD
Earlier steps:
- Launch and connect to EC2: https://youtu.be/JH5V5jKzILs
- Create the Security Group first: https://youtu.be/3tn56NQ2710
- Install Node.js 22 and Git on EC2: https://youtu.be/ixzhrVpSSlU
- Connect EC2 to GitHub with SSH keys: https://youtu.be/au71kakla4M
- Clone a private GitHub repo on EC2: https://youtu.be/1MDz3yMzj7A
- Run the Node.js app with npm start: https://youtu.be/ophx-eD7yQQ
- Keep it running 24/7 with PM2: https://youtu.be/aWgdEx5-w1k
- Install Nginx on Ubuntu: https://dev.to/jahidhiron/install-nginx-on-ubuntu-aws-ec2-start-it-enable-it-check-it-bjb