Nginx Reverse Proxy Configuration
Basic Nginx setup to proxy requests to a backend service and serve static files.
When deploying a Node or Python app, you often place a reverse proxy in front to handle SSL termination, compression, and static assets. This Nginx configuration listens on port 80, proxies /api
requests to an upstream service, and serves static files from a public
directory. Adjust paths and upstream hostnames for your environment.
server {
listen 80;
server_name example.com;
# Serve static files
location / {
root /var/www/public;
try_files $uri $uri/ =404;
}
# Proxy API requests
location /api/ {
proxy_pass http://backend: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;
}
}
Save this as a file like /etc/nginx/conf.d/app.conf
and reload Nginx. You can add SSL termination with listen 443 ssl;
and certificates when ready for HTTPS. This pattern decouples your application server from web server concerns, improving security and scalability.