0%
NGINX Configuration Cheatsheet

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 to root defined in the server 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.