05/10/2026
nginx from scratch: one conf file for a reverse proxy
Four nginx blocks, a sample nginx.conf for a reverse proxy, and a few habits worth having before you reload.
nginx sits in front of the app. It accepts the request, finishes TLS when you ask it to, and forwards the work to the process that actually builds the response. It can serve static files too; day to day it is mostly a reverse proxy.
One master process supervises the workers. worker_processes auto starts one worker per CPU. Each worker holds connections with events, not with one thread per request.
Four blocks
A directive is valid only inside its own block. Put limit_req_zone inside server and nginx -t fails.
events— connection ceiling for each workerhttp— settings shared by every siteserver— one name, or one portlocation— one path inside that site
Sample file
This is a starting point, not a production drop-in. Change the name, the certificate paths, and the upstream address. On Debian/Ubuntu the main file is /etc/nginx/nginx.conf; this sample keeps every block in one file so you can see how they nest.
# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 16m;
gzip on;
gzip_types text/css application/json application/javascript;
log_format main '$remote_addr $status "$request"';
access_log /var/log/nginx/access.log main;
# empty Upgrade: clear Connection so upstream keepalive works
# non-empty Upgrade: pass "upgrade" for WebSocket
map $http_upgrade $connection_upgrade {
default upgrade;
'' '';
}
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
upstream app {
server 127.0.0.1:3000;
keepalive 16;
}
server {
listen 80 default_server;
server_name _;
return 444;
}
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/certs/fullchain.pem;
ssl_certificate_key /etc/nginx/certs/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
add_header Strict-Transport-Security "max-age=31536000" always;
add_header X-Content-Type-Options nosniff always;
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}
location = /login {
limit_req zone=login burst=8 nodelay;
proxy_pass http://app;
proxy_http_version 1.1;
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;
proxy_set_header Connection "";
}
location / {
proxy_pass http://app;
proxy_http_version 1.1;
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;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}If the user line errors, change it to the account your distro uses. Keep the HSTS header only after a real certificate is on that name; with a test cert the browser stays pinned to HTTPS for a while.
Test and reload
nginx -t && nginx -s reloadnginx -t checks syntax and changes nothing. A reload lets old workers finish open requests. nginx -T prints the full effective config, including every include.
Practical notes
- A proxied request uses two connections. One toward the browser, one toward
upstream. Soworker_connections 1024is about 512 concurrent proxied requests per worker, not 1024 users. - `location =` is exact.
= /healthzmatches only that path.location /healthalso matches anything that starts with/health, including/healthz. - A trailing slash on `proxy_pass` rewrites the path.
proxy_pass http://app;forwards the URI unchanged.proxy_pass http://app/;strips thelocationprefix from the URI. - Set the forwarding headers yourself. Without
HostandX-Forwarded-Protothe app thinks the request arrived athttp://127.0.0.1and builds the wrong redirect. - `server_tokens off`. The nginx version stays out of the
Serverheader and error pages. - Rate-limit the sensitive path.
limit_reqbelongs on login and forms. Put it on the whole site and normal pages return429too.limit_req_zoneitself belongs inhttp. - Do not serve an unknown Host.
default_serverwithreturn 444closes a request that came in by raw IP.444is nginx’s own code; the client gets no response body. - `client_max_body_size` defaults to 1 megabyte. A larger body returns
413. If you have no uploads, make the 16m in this sample smaller. - A `502` is usually not nginx. The upstream is down, or port
3000is wrong. Check the app first, then the config.