Nginx include files, variables and inherited settings
Split configuration into smaller files
The include directive reads another configuration file or files matching a pattern. Use it to organize settings while keeping their original context. The included text is parsed at the point of inclusion: it is not a separate process, a new scope or a later request-time action.
http { include conf.d/*.conf;}server { listen 127.0.0.1:18083; server_name localhost; root html;}This example expects conf.d alongside the main configuration file. Relative include filenames are resolved from the configuration prefix (the directory containing the main configuration file), which can differ from the runtime prefix specified with -p. A file matching the pattern must contain entries valid at the inclusion point. For example, an extra http wrapper in site.conf would be invalid here. Use explicit filenames for a small lab; do not rely on wildcard file order for application semantics.
Variables are not shell environment substitutions
location = /request-path { default_type text/plain; return 200 "path=$uri\n";}The return directive in the HTTP rewrite module sends an immediate response in this example. $uri represents the current normalized URI. A request to /request-path?mode=demo should return the text path=/request-path followed by a newline. The query string is separate from the URI value used here. $request_uri, by contrast, retains the original request URI including its arguments. Some directives accept variables; others require literal configuration-time values. Declaring a shell environment variable does not automatically make it an Nginx request variable.
How settings are inherited
server { listen 127.0.0.1:18083; root html; location / { } location /assets/ { root alternate; }}For this root example, a request for /hello.txt uses the server root html because its selected location has no root of its own. A request for /assets/logo.txt uses alternate/assets/logo.txt instead, relative to the lab runtime prefix. The root is joined with the URI path; it does not strip the /assets/ prefix. Each directive has its own rules: some inherit when absent, some replace a parent list, and some have different merge behavior. Do not infer inheritance from braces alone or assume every repeated directive is accepted.
Troubleshoot includes and values
http { server { listen 127.0.0.1:18083; }}The outer http is invalid at that inclusion point. Remove that wrapper from the fragment, not from the main configuration. If a variable is unrecognized, check the module, spelling, supported directive arguments and installed build. If an inherited setting surprises you, consult the specific directive and trace the selected server/location.
Only include reviewed lab-owned configuration files. Do not include real secrets, package module directories or unreviewed generated files. Testing a configuration is not proof of its request routing or safety.
Practice and next steps
Use the Chapter 3 offline fixture to predict the response for /hello.txt, /assets/logo.txt and /request-path?mode=demo. Identify the include location, selected root and variable value. The next chapter covers CLI validation, process lifecycle and safe reload.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Includes, variables and inheritance
Check the main concepts taught in this lesson.
What context applies to an included fragment?
Which path is the basis of a relative include?
For /request-path?mode=demo, what does $uri contain in the example?
With root alternate in location /assets/, where does /assets/logo.txt map?
Which rule applies to inheritance?