Sub topic 4
How the Configuration Is Organized
Assume you can use the Linux command line, edit files with sudo, and understand basic HTTP requests, DNS names, and TCP ports. Nginx configuration is a hierarchy of contexts. A context is a section where particular directives are valid. A directive is an instruction such as listen, root, or proxy_pass.
The configuration hierarchy
The main configuration file is usually /etc/nginx/nginx.conf. Distribution packages often place site-specific files in /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/. The main file can load those files with an include directive.
The common contexts are:
- Main context: The outermost level. It contains global settings such as the user, worker processes, and error log.
eventscontext: Settings for network connections, such as the maximum connections handled by each worker.httpcontext: Settings for HTTP servers. It can contain logging rules, MIME types, compression settings, upstream groups, andserverblocks.upstreamcontext: A named group of application servers used for proxying and load balancing.servercontext: One virtual host. It selects requests by listening address, port, and host name.locationcontext: Rules for a URI path, such as/images/or/api/.
A directive belongs inside a context that supports it. For example, listen belongs in a server block, while proxy_pass belongs in a location block. Nginx reports an error when a directive appears in an invalid context.
A small configuration
This is a minimal structure that serves files from one host name:
user www-data;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
server {
listen 80;
server_name example.com;
root /var/www/example.com/html;
location / {
try_files $uri $uri/ =404;
}
}
}The semicolon ends each directive. Braces define contexts. Whitespace and indentation make the file easier to read, but they do not change its meaning.
The http context supplies defaults to its child server blocks, and a server context can supply settings to its child location blocks. A child setting usually overrides an inherited parent setting. However, inheritance differs between directives. Check the directive documentation when behavior matters.
Use include to split a large configuration into manageable files:
http {
include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
}The wildcard includes matching files in that directory. The included text is treated as if it appeared at that location, so its directives must be valid inside the current context.
Inspecting the effective configuration
Before editing, inspect both the main file and included files. The -T option prints the complete configuration that Nginx loads:
sudo nginx -T
sudo nginx -tThe first command helps you find duplicate or unexpected settings. The second checks syntax and file references without applying changes.
Exercise
Exercise
In which context should you place server_name, and how can you verify that an included file is actually loaded?
Show answer
Place server_name in a server context. Run sudo nginx -T and search its output for the file's contents.