What I Wanted to Do
I needed to serve a static site and set up a reverse proxy with nginx, but the config file syntax wasn’t obvious.
Basic Config File
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Reverse Proxy Config
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;
}
}
Test and Reload Config
nginx -t # Syntax check
nginx -s reload # Reload without downtime
Common Pitfalls
- Always run
nginx -tbefore reloading — a syntax error will take down the server - Missing semicolons
;are a frequent cause of errors - Before exposing nginx publicly, open ports 80 and 443 in the firewall
For firewall setup, see Linux UFW Firewall Basics.
Related Posts
- nginx 502 Bad Gateway: Causes and Fixes
- nginx Reverse Proxy Setup (Serving a Node.js App)
- Enable gzip Compression in nginx
- nginx SSL with Let’s Encrypt (certbot)
Recommended Cloud Hosting
Looking for reliable cloud infrastructure? Check out these developer-friendly services.
- Cherry Servers - High-performance VPS and dedicated servers
- Cloudways - Managed cloud hosting for developers