Nginx as a Reverse Proxy
Nginx is a high-performance web server commonly used as a reverse proxy, load balancer, and SSL terminator.
Why Use a Reverse Proxy?
- Hides backend servers from the public internet
- Terminates SSL in one place
- Enables load balancing across multiple instances
- Serves static files efficiently
Basic Reverse Proxy Config
nginx
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}SSL Termination with Certbot
bash
# Install Certbot
sudo apt install certbot python3-certbot-nginx
# Obtain and install certificate
sudo certbot --nginx -d example.comLoad Balancing
nginx
upstream backend {
server app1:3000;
server app2:3000;
server app3:3000;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}