What is Nginx?
Nginx (engine-x) is a web server commonly used as a reverse proxy, load balancer, and static HTTP server.
In this pipeline it sits between users and your Node app:
User → Nginx (80/443) → Node App (3000) → Nginx → User
Users never need :3000 in the URL.
Why Use It?
- Hide internal ports
- Clean URLs (
https://your-domain.com) - Better security and traffic handling
- Easy path to HTTPS (next lesson)
Install Nginx
sudo apt update
sudo apt install nginxConfigure Reverse Proxy
Edit the site config (Ubuntu default):
sudo vim /etc/nginx/sites-available/defaultExample server block:
server {
listen 80;
server_name your-domain.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;
}
}Replace your-domain.com with your domain or EC2 public DNS while testing.
Quick Vim reminders
| Keys | Action |
|---|---|
i | Insert mode |
Esc | Leave insert mode |
:wq | Save and quit |
:q! | Quit without saving |
Restart Nginx and run the app
sudo systemctl restart nginx
pm2 start app.js # or pm2 restart appNow:
- App listens on
localhost:3000 - Public traffic hits port 80 via Nginx
Final Flow
Browser Request
↓
Nginx (Port 80)
↓
Node App (Port 3000)
↓
Response back to user
Nginx hides your app port and forwards user requests to the backend with a reverse proxy.