Directives and Syntax
Every line of Nginx configuration is built from directives. Knowing the syntax rules prevents most config errors.
Simple Directives
A simple directive is a name followed by parameters, ending with a semicolon:
listen 80;
server_name example.com www.example.com;
root /var/www/html;
The semicolon is required. Forgetting it is the most common mistake, and nginx -t will report it.
Block Directives
A block directive (also called a context) groups other directives inside curly braces { }:
server {
listen 80;
location / {
root /var/www/html;
}
}
Block directives do not end with a semicolon. They end with the closing brace.
Comments
Use # for comments. Everything after it on the line is ignored:
# This serves the main website
listen 80; # default HTTP port
Whitespace
Nginx ignores extra spaces and blank lines, so indent for readability. The structure comes from braces and semicolons, not indentation.
Quoting Values
Wrap values in quotes when they contain spaces or special characters:
add_header X-Custom "hello world";
Master these rules and the rest of Nginx configuration becomes much easier to read.