Commands
sudo nginx -t # test the config (always before reload)sudo nginx -T # test, and print the whole configsudo systemctl reload nginx # apply changes, no downtimesudo systemctl restart nginx # full restartnginx -v # versionnginx -V # version, build options and modulessudo tail -f /var/log/nginx/error.logWhere the config lives
| Path | What |
|---|---|
/etc/nginx/nginx.conf |
Main file, includes the rest |
/etc/nginx/conf.d/*.conf |
Site files (official packages, RHEL) |
/etc/nginx/sites-available/ |
Site files (Debian, Ubuntu)… |
/etc/nginx/sites-enabled/ |
…enabled with a symlink |
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/A reverse proxy
server { listen 80; server_name app.example.com;
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; }}WebSockets also need, in that location:
proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";HTTPS
server { listen 443 ssl; http2 on; # nginx 1.25.1+ server_name app.example.com; ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;}
server { listen 80; server_name app.example.com; return 301 https://$host$request_uri;}sudo certbot --nginx -d app.example.com -d www.app.example.comsudo certbot renew --dry-runRead a certificate’s names and dates with the SSL Certificate Decoder.
Static files and single-page apps
root /var/www/app/dist;index index.html;
location / { try_files $uri $uri/ /index.html; # SPA: unknown paths → index.html}
location /assets/ { expires 1y; add_header Cache-Control "public, immutable";}Password protection
location /admin/ { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;}sudo htpasswd -B -c /etc/nginx/.htpasswd anna # -c creates the file: only for the first userOr make the lines in your browser with the htpasswd Generator.
Limits and timeouts
client_max_body_size 50m; # uploads (default 1m → 413)proxy_connect_timeout 5s;proxy_read_timeout 120s; # slow backends (default 60s → 504)proxy_buffer_size 16k; # big headers from the backendproxy_buffers 8 16k;large_client_header_buffers 4 16k;server_names_hash_bucket_size 128;Logs with timings
log_format timed '$remote_addr "$request" $status ' 'rt=$request_time urt=$upstream_response_time';access_log /var/log/nginx/access.log timed;error_log /var/log/nginx/error.log warn;awk '{print $3}' /var/log/nginx/access.log | sort | uniq -c | sort -rn # count by statusThe $3 is the status in the timed format above; in the default format, it’s $9. The Log Analyzer reads both formats and does the counting for you. In the official Docker image, access.log and error.log are links to the container’s output: read them with docker logs, or log to other file names.
Which location wins
| Form | Matches |
|---|---|
location = /health |
Exactly this path, checked first |
location ^~ /static/ |
This prefix, and stops looking at regexes |
location ~ \.php$ |
Regex, case-sensitive, first match in file order |
location ~* \.(jpg|png)$ |
Regex, case-insensitive |
location /api/ |
Longest prefix, used if no regex matches |
Common errors
502 Bad Gateway504 Gateway Time-out403 Forbidden413 Request Entity Too Largeupstream sent too big headercould not build server_names_hash499 client closed requestbind() … Address already in use
Generate a full server block with the nginx Config Generator.