Nginx for Developers: Reverse Proxy and Load Balancing
You ran node server.js on port 3000. You are done, right?
No. You need Nginx in front.
Why Nginx?
- Security: Hides your backend.
- Performance: Serves static files (images, css) much faster than Node.
- SSL: Handles HTTPS encryption.
- Load Balancing: Distributes traffic.
Basic Config (/etc/nginx/sites-available/default)
server {
listen 80;
server_name example.com;
# Serve Static Files
location /static/ {
root /var/www/app;
expires 30d;
}
# Reverse Proxy to Node.js
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;
proxy_cache_bypass $http_upgrade;
}
}
Load Balancing
Run your app on ports 3000, 3001, 3002.
upstream myapp {
server localhost:3000;
server localhost:3001;
server localhost:3002;
}
server {
location / {
proxy_pass http://myapp;
}
}
Nginx will distribute requests round-robin style.
HTTPS with Certbot
Don't buy certs. Use Let's Encrypt.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com
It auto-configures SSL in your Nginx file.
Nginx is the "glue" of the internet. Even basic knowledge separates you from the average dev.