Nginx as a Reverse Proxy: Complete Configuration Guide
Nginx is one of the most widely used web servers and reverse proxies. It handles load balancing, SSL termination, caching, and request routing with exceptional performance.
Installing Nginx
# Ubuntu / Debian
sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
# Verify
nginx -v
curl http://localhost
Basic Reverse Proxy
Forward requests to a backend application:
server {
listen 80;
server_name myapp.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
SSL with Let's Encrypt
# Install Certbot
sudo apt install certbot python3-certbot-nginx
# Obtain certificate
sudo certbot --nginx -d myapp.example.com
# Auto-renewal is set up by default; verify with:
sudo certbot renew --dry-run
After Certbot, your config will look like:
server {
listen 443 ssl http2;
server_name myapp.example.com;
ssl_certificate /etc/letsencrypt/live/myapp.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1: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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
listen 80;
server_name myapp.example.com;
return 301 https://$server_name$request_uri;
}
Load Balancing
Distribute traffic across multiple backend servers:
upstream backend {
least_conn;
server 10.0.1.10:3000;
server 10.0.1.11:3000;
server 10.0.1.12:3000;
}
server {
listen 443 ssl http2;
server_name myapp.example.com;
location / {
proxy_pass http://backend;
}
}
Load balancing methods:
round-robin(default) — requests rotate through serversleast_conn— send to the server with fewest active connectionsip_hash— sticky sessions based on client IPhash— custom hash key
Caching
Cache backend responses to reduce load:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=app_cache:10m max_size=1g inactive=60m;
server {
location / {
proxy_pass http://backend;
proxy_cache app_cache;
proxy_cache_valid 200 10m;
proxy_cache_valid 404 1m;
add_header X-Cache-Status $upstream_cache_status;
}
location /api/ {
proxy_pass http://backend;
proxy_cache off;
}
}
Rate Limiting
Protect against abuse:
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://backend;
}
}
Security Headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Useful Commands
nginx -t # Test configuration
sudo nginx -s reload # Reload without downtime
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log
Nginx is the gateway to your application. A well-configured reverse proxy improves performance, security, and reliability.
Enjoyed this article?
Get more DevOps insights delivered to your inbox.
Get new posts by email
Subscribe to get an email when a new blog post is published. Skip anytime.
No spam, unsubscribe anytime.
Written by
Priya Kapoor
Tech enthusiast and creative writer sharing insights on modern development.
View all postsDiscussion
0 comments
Sign in to join the conversation.
Be the first to comment
Start a conversation about this post
