
NGINX Configuration Cheatsheet
๐ NGINX Cheatsheet: Core Configuration Only
๐ 1. Basic NGINX Configuration Structure
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
include /etc/nginx/sites-enabled/*;
}
The http
block includes all HTTP-specific config, including virtual hosts from sites-enabled
.
๐ 2. Minimal Server Block for a Static Website
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
This serves static files for example.com
from /var/www/example.com/html
.
๐งฑ 3. Location Block Basics
location / {
try_files $uri $uri/ =404;
}
location /images/ {
root /var/www/assets;
}
/
serves files relative toroot
defined in theserver
block./images/
serves files from/var/www/assets/images
.
๐งญ 4. Index and Default File Settings
index index.html index.htm;
If a user visits /about/
, NGINX looks for /about/index.html
or /about/index.htm
.
๐ 5. Serve Static Content from Custom Path
location /static/ {
root /var/www/site;
}
Resolves
/static/logo.png
to/var/www/site/static/logo.png
.
Using alias
:
location /static/ {
alias /var/www/site/;
}
Resolves
/static/logo.png
to/var/www/site/logo.png
.
Use alias
when the path on disk doesn’t match the URL path.
๐ชช 6. Enabling a Site (Using sites-available)
sudo ln -s /etc/nginx/sites-available/example.conf /etc/nginx/sites-enabled/
Then reload:
sudo nginx -t && sudo systemctl reload nginx
This makes your site live using clean modular config structure.
๐งช 7. Test Config and Reload
sudo nginx -t # Test syntax
sudo systemctl reload nginx # Apply changes
Always test before reloading to avoid downtime due to config errors.
Tags:
nginx
configuration
web server