Skip to main content

Config Syntax Reference

Learning Focus

Leave this lesson with a working understanding of config syntax reference that you can apply immediately in production.

Complete directive reference for the most common Nginx configuration needs.


nginx.conf — Core Template

/etc/nginx/nginx.conf
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
pid /run/nginx.pid;

events {
worker_connections 1024;
multi_accept on;
use epoll;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr [$time_local] "$request" $status $body_bytes_sent $request_time';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
server_tokens off;

gzip on;
gzip_types text/plain text/css application/json application/javascript
text/xml image/svg+xml;
gzip_min_length 1024;
gzip_vary on;

client_max_body_size 64m;
client_body_buffer_size 128k;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

Server Block — Full Template

/etc/nginx/conf.d/example.com.conf
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com www.example.com;

ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_stapling on;

root /var/www/example.com/public;
index index.php index.html;

access_log /var/log/nginx/example.com.access.log main;
error_log /var/log/nginx/example.com.error.log warn;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
try_files $uri =404;
include fastcgi.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
}

location ~* \.(css|js|png|jpg|jpeg|gif|ico|webp|svg|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}

location ~* \.(env|log|ini|bak|sql|sh)$ { deny all; }
location ~ /\. { deny all; }
}

Location Matching Priority

location = /exact { } # Exact — highest priority
location ^~ /prefix/ { } # Prefix — stops regex search
location ~ \.php$ { } # Case-sensitive regex
location ~* \.(jpg|png) { } # Case-insensitive regex
location /fallback/ { } # Regular prefix
location / { } # Catch-all — lowest priority

Directive Quick Reference

HTTP Context

DirectiveExamplePurpose
sendfileonKernel file sending
tcp_nopushonBatch headers + body
keepalive_timeout65Keep-alive seconds
gziponEnable compression
server_tokensoffHide Nginx version
client_max_body_size64mMax upload size

Server Context

DirectiveExamplePurpose
listen443 ssl http2Port + SSL + HTTP/2
server_nameexample.comMatched hostnames
root/var/www/site/publicDocument root
ssl_certificate/etc/ssl/certs/...TLS cert file
ssl_protocolsTLSv1.2 TLSv1.3Allowed TLS versions
return301 https://...Redirect
add_headerX-Frame-Options SAMEORIGINResponse header

Location / Proxy Context

DirectiveExamplePurpose
try_files$uri $uri/ =404File fallback chain
proxy_passhttp://127.0.0.1:3000Upstream proxy
fastcgi_passunix:/run/php/...PHP-FPM pass
expires1yBrowser cache
deny allBlock access
allow192.168.1.0/24Allow IP range
proxy_set_header Host$hostForward hostname
proxy_set_header X-Real-IP$remote_addrReal client IP
proxy_read_timeout60sRead timeout

Built-In Variables

VariableValue
$hostRequest Host header
$remote_addrClient IP
$schemehttp or https
$request_uriFull URI with query string
$uriURI path only
$argsQuery string
$document_rootCurrent root value
$fastcgi_script_namePHP script path
$http_upgradeUpgrade header value
$request_timeTotal request time (s)
$upstream_response_timeBackend response time
$upstream_cache_statusHIT, MISS, BYPASS