Nginx Basics for Developers
Nginx is a web server and reverse proxy. You will see it in many deployment setups because it is good at sitting in front of web projects. It can serve static files directly. It can forward requests to an application running on another port. It can handle redirects, TLS certificates, compression, caching headers, and basic traffic routing. For small projects, you do not need to know every Nginx feature. You need to understand the common shape.
The Mental Model
Think of Nginx as the public front door. The browser sends a request to: ```plain text https://example.com
Nginx receives it and decides what to do.
It might:
- serve a static file from disk
- forward the request to a backend app
- redirect HTTP to HTTPS
- return a 404
- block or limit a request
Your app does not always need to listen directly on the public internet.
Nginx can do that part.
## Serving Static Files
For a static site, Nginx can serve generated files from a directory.
Conceptually:
```plain text
server {
server_name example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
The important pieces are:
- `server_name` matches the domain
- `root` points to the static files
- `index` defines the default file
- `try_files` tells Nginx how to resolve paths If your static site builds into an `output/` directory, deployment may copy that output to the server path Nginx serves.
Reverse Proxy
For an app server, Nginx often forwards traffic to a local port. Example: ```plain text server { server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:8000;
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;
}
}
In this setup:
- Nginx listens publicly
- the app listens locally
- Nginx forwards requests to the app
This is common for Django, FastAPI, Go services, Node apps, and many other backends.
## Server Blocks
Nginx uses server blocks to define site behavior.
One server can host multiple domains:
```plain text
example.com
api.example.com
admin.example.com
Each can have its own server block. That makes Nginx useful for small VPS setups where one server hosts several small projects. The main thing is to keep each domain's config understandable. Do not let one huge config file become a dumping ground.
HTTPS
Modern sites should use HTTPS. Nginx usually works with TLS certificates from Let's Encrypt or another certificate provider. A common setup includes:
- redirect HTTP to HTTPS
- serve TLS certificates
- renew certificates automatically Tools like Certbot are often used to request and renew certificates. The exact commands change by system, but the goal is stable:
Public web traffic should use HTTPS, and renewal should be automated. Manual certificate renewal is easy to forget.
Logs
Nginx logs are extremely useful. Common log files: ```plain text /var/log/nginx/access.log /var/log/nginx/error.log
Access logs show requests.
Error logs show problems Nginx encountered.
If a site is down, these logs help answer:
- did the request reach the server?
- did Nginx find the file?
- did proxying fail?
- is there a permission issue?
- is the upstream app unavailable?
That is often faster than guessing.
## Common Mistakes
### Mistake 1: Confusing Nginx errors with app errors
A \`502 Bad Gateway\` often means Nginx could not reach the upstream app.
That is different from your app returning a normal \`500\` error.
Check both Nginx logs and app logs.
### Mistake 2: Wrong file permissions
Nginx needs permission to read static files.
If files exist but return forbidden errors, permissions may be the issue.
### Mistake 3: Forgetting to reload Nginx
After changing config, test and reload:
```bash
sudo nginx -t
sudo systemctl reload nginx
If you edit config but never reload, nothing changes.
Where This Shows Up in Real Projects
Nginx is useful for:
- serving static sites
- proxying app servers
- routing subdomains
- handling HTTPS
- redirecting old URLs
- keeping app ports private For a personal site, Nginx may simply serve generated static files. For a Django or Go app, it may sit in front of an application server. Either way, it is worth learning the basics because it appears in many deployment paths.
Key Takeaways
- Nginx can serve static files and proxy app servers.
- Server blocks define behavior for domains.
- Reverse proxies let apps run on internal ports.
- HTTPS and certificate renewal should be handled deliberately.
Nginx logs are often the fastest way to debug deployment problems.
Related Articles
VPS Deployment Basics for Developers
- Docker Compose for Small Projects
- Cloudflare Basics for Personal Sites