Introduction
Before configuring Nginx, it's essential to understand the layout of Nginx configuration files and the
various definitions that relate to Nginx. This guide will walk you through directives, contexts,
location context modifiers, and the try_files directive. Understanding these concepts is crucial for
reading and writing effective Nginx configuration files.
Key Concept: Nginx consists of modules controlled by directives specified in
configuration files. Mastering these directives is fundamental to effective Nginx administration.
Understanding Directives
Directives are the building blocks of Nginx configuration. A directive consists of an option or parameter
name followed by the option or parameter value, and all directives end with a semicolon.
Directive Structure
The basic syntax of a directive is:
directive_name directive_value;
Examples of Directives
listen 80;
server_name example.com www.example.com;
root /var/www/html;
index index.html index.php;
Important: All directives must end with a semicolon (;). Forgetting the semicolon is a
common syntax error.
Understanding Contexts
A context is also a directive, but it encloses other directives within its braces (curly brackets).
Contexts are sometimes referred to as context blocks and provide organizational structure to Nginx
configurations.
Context Structure
context_name {
directive1 value1;
directive2 value2;
}
Main Nginx Contexts
Context Descriptions
| Context |
Description |
Usage |
| main |
The only context not contained within curly brackets |
Configures details that affect the entire Nginx application |
| events |
Contained within the main context |
Sets global options for connection handling |
| http |
Contained within the main context |
Enables and configures the HTTP service |
| server |
Contained within the http context |
Contains configuration for virtual hosts (server blocks) |
| location |
Contained within the server context |
Configures how Nginx responds to requests for resources |
Complete Configuration Example
user www-data;
worker_processes auto;
events {
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com;
root /var/www/example.com;
location / {
try_files $uri $uri/ =404;
}
}
}
Context Inheritance: Child contexts can override directives from parent contexts. For
example, a directive in the server context can override a directive in the http context.
Testing and Reloading Nginx
After making changes to any Nginx configuration file, you must follow a specific workflow to ensure your
changes are valid and applied correctly.
Configuration Workflow
1. Edit Configuration
→
2. Test Syntax
→
3. Fix Errors (if any)
→
4. Reload Nginx
Commands
sudo nano /etc/nginx/sites-available/default
sudo nginx -t
sudo systemctl reload nginx
sudo systemctl restart nginx
Example Error Handling
$ sudo nginx -t
nginx: [emerg] unknown directive "routes" in /etc/nginx/sites-available/default:41
nginx: configuration file /etc/nginx/nginx.conf test failed
$ sudo nano -L /etc/nginx/sites-available/default
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl reload nginx
Critical Rule: Never reload Nginx without testing the configuration first. A syntax
error can prevent Nginx from starting, causing downtime for all hosted sites.
Location Context and Modifiers
The location context is one of the most important and complex parts of Nginx configuration. It determines
how Nginx responds to requests for specific resources within the server.
Understanding Request Matching
When a request is made to your server, Nginx matches it to a location context based on the request URI
(the part of the URL after the domain name).
Request URI Example
https://example.com/images/photo.jpg
The request URI is: /images/photo.jpg
Location Modifiers
Modifiers determine how Nginx matches the location block to the request URI. Understanding these
modifiers is crucial for proper Nginx configuration.
-
=
Exact Match: Matches the exact URI only. Highest priority.
-
^~
Prefix Value Match: Matches URI starting with the specified prefix. Processed
before regex matches.
-
~
Case-Sensitive Regex: Matches using case-sensitive regular expression.
-
~*
Case-Insensitive Regex: Matches using case-insensitive regular expression.
-
(none)
Prefix Match: Matches URI starting with the specified string. Lowest priority.
Modifier Priority Order
Processing Order
1. Exact Match (=) - Highest Priority
↓
2. Prefix Value Match (^~)
↓
3. Regular Expression (~ or ~*) - First match wins
↓
4. Prefix Match (no modifier) - Lowest Priority
Modifier Examples
1. Exact Match (=)
location = /xmlrpc.php {
return 403;
}
2. Prefix Value Match (^~)
location ^~ /images/ {
expires 30d;
access_log off;
}
3. Case-Sensitive Regex (~)
location ~ \.ico$ {
access_log off;
log_not_found off;
}
4. Case-Insensitive Regex (~*)
location ~* \.txt$ {
return 403;
}
location ~* ^/uploads/.*\.php$ {
return 403;
}
5. Prefix Match (no modifier)
location /tax {
return 301 https://example.com/taxes;
}
Complex Example: Multiple Locations
server {
listen 80;
server_name example.com;
location = /xmlrpc.php {
return 403;
}
location ^~ /static/ {
expires 1y;
add_header Cache-Control "public, immutable";
}
location ~* ^/uploads/.*\.php$ {
return 403;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
The try_files Directive
The try_files directive is one of the most powerful and commonly used directives in Nginx. It checks for
the existence of files in the specified order and uses the first found file for request processing.
Basic Syntax
try_files file1 file2 ... fallback;
How try_files Works
try_files Processing Flow
Request: example.com/image.jpg
↓
Check 1: Does /var/www/html/image.jpg exist?
↓ (if no)
Check 2: Does /var/www/html/image.jpg/ directory exist?
↓ (if no)
Fallback: Use fallback option or return 404
Common try_files Patterns
1. Basic Static File Serving
location / {
try_files $uri $uri/ =404;
}
2. WordPress Pretty Permalinks
location / {
try_files $uri $uri/ /index.php?$args;
}
3. Static Page Caching (W3 Total Cache)
location / {
try_files $uri $uri/
/wp-content/cache/page_enhanced/$host$uri/_index.html $uri $uri/ /index.php?$args;
}
4. PHP Security Check
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
Real-World Example: Complete WordPress Configuration
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.php index.html;
location = /xmlrpc.php {
return 403;
}
location = /wp-config.php {
deny all;
}
location ~*
\.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, immutable";
access_log off;
}
location ~* ^/wp-content/uploads/.*\.php$ {
return 403;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
Best Practice: The try_files directive is essential for modern web applications. It
enables clean URLs, improves performance through caching, and enhances security by validating file
existence before processing.
Common Nginx Variables
Nginx provides numerous variables that can be used in configuration files. Understanding these variables
is crucial for effective configuration.
| Variable |
Description |
Example Value |
$uri |
The current URI in the request (normalized) |
/images/photo.jpg |
$args |
Query string arguments |
id=123&page=2 |
$document_root |
The root directory for the current request |
/var/www/html |
$host |
Host name from the request |
example.com |
$request_uri |
Full original request URI with arguments |
/page?id=123 |
$scheme |
Request scheme (http or https) |
https |
Configuration Best Practices
Essential Guidelines
- Always test before reloading: Use
sudo nginx -t before
sudo systemctl reload nginx
- Use comments liberally: Document your configuration with comments (lines
starting with #)
- Follow the principle of least privilege: Only grant necessary permissions
- Keep security in mind: Block sensitive files and directories
- Optimize static content delivery: Use appropriate caching headers
- Use specific location blocks: More specific matches process faster
- Avoid duplicate directives: Don't place the same directive twice in the same
context
- Place directives in correct contexts: Directives only work in their intended
contexts
Common Mistakes to Avoid
- Forgetting semicolons at the end of directives
- Reloading Nginx without testing configuration first
- Using incorrect context for directives
- Mixing up location modifier priorities
- Not validating file existence before PHP processing
- Exposing sensitive configuration files
- Using overly broad regex patterns
Conclusion
Understanding Nginx configuration is fundamental to effectively managing web servers. By mastering
directives, contexts, location modifiers, and the try_files directive, you can create secure, efficient,
and maintainable Nginx configurations.
Key Takeaways
- Directives are the building blocks of Nginx configuration and must end with semicolons
- Contexts organize directives hierarchically: main → events/http → server → location
- Child contexts can override parent context directives
- Location modifiers determine matching priority: = → ^~ → ~/~* → (no modifier)
- The try_files directive enables efficient file serving and clean URLs
- Always test configuration with
nginx -t before reloading
- Understanding these concepts is essential before optimizing and hardening your hosting stack
Next step: Harden and optimize your
hosting stack components, starting with Nginx.