NGINX Certified Administrator - Management part 2

Table of Contents

  1. Introduction
  2. NGINX Configuration File Structure and Default Locations
  3. Merging Configurations: Use of Include Directives and Merge Order
  4. Directive Inheritance in NGINX Contexts
  5. Overriding and Special Behaviors of Array and Command Directives
  6. Practical Considerations and Best Practices
  7. Conclusion

1. Introduction

NGINX is renowned for its high performance and flexibility as a web server, reverse proxy, and load balancer. A key component of its flexibility comes from the configuration system, which blends a main configuration file with numerous included files to create a cohesive active configuration. This article explores the order in which configuration files are merged together and explains how directive inheritance, including overriding mechanisms, works within various NGINX contexts. Understanding these concepts is pivotal for system administrators and developers who need to maintain clear, efficient, and dependable NGINX configurations in environments ranging from single-server deployments to complex distributed systems.


2. NGINX Configuration File Structure and Default Locations

NGINX uses a text‑based configuration file system that is both human-friendly and precise. The central configuration file, commonly known as nginx.conf, is the starting point of the configuration hierarchy. For NGINX Plus, this file is typically found in the /etc/nginx directory, while in NGINX Open Source installations, it might be located in directories such as /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx depending on the packaging system and operating system.

The configuration file is composed of directives that fall into several distinct contexts:

The clear separation into contexts facilitates modular configurations, where common values can be set at a high level and inherited by nested contexts, thus reducing redundancy and promoting easier maintenance. For instance, a log format defined in the http block can be utilized by all virtual servers unless a specific override is needed at the server or location level.

A typical configuration file might also include additional files using the include directive. This allows administrators to split configurations into feature‑specific files, such as those for HTTP features, TCP/UDP streams, or load balancing settings, making the overall system more modular and manageable.


3. Merging Configurations: Use of Include Directives and Merge Order

One of the pivotal components of the NGINX configuration system is the ability to include external configuration files using the include directive. This approach allows administrators to structure their configurations into multiple smaller files rather than one large monolithic file. The recommended practice is to store feature‑specific files in directories like /etc/nginx/conf.d or to use a combination of sites-available and sites-enabled for organizing virtual server definitions.

3.1 How the Include Directive Works

The include directive inserts the contents of one file into another at the point where the directive appears. For example, adding the following lines in nginx.conf:

include conf.d/http;  
include conf.d/stream;  
include conf.d/exchange-enhanced;  

...will cause NGINX to process the contents of each of these files sequentially within the context in which they are included.

When using wildcard patterns, like:

include /etc/nginx/conf.d/*.conf;  

NGINX will include all files that match the pattern, processing them in alphabetic order. This means that if multiple files define similar or overlapping settings, the order of inclusion can impact the final combined configuration. The merge order is generally as follows:

  1. Processing the Main File: NGINX reads the primary nginx.conf file.
  2. Inclusion of Specified Files: Any file referenced by an include directive is merged into the configuration in the exact place where the directive appears.
  3. Alphabetical Order for Wildcards: Files matched by a wildcard are processed alphabetically unless a specific order is enforced by naming conventions.

This merging mechanism provides a powerful way to dynamically structure configurations. For example, administrators may place incremental changes or environment-specific directives into separate files without modifying the core configuration, hence reducing the risk of errors when updating configurations.

3.2 Table: Merge Order of NGINX Included Files

The following table summarizes the merge order when using included configuration files:

Order Stage Description Example Configuration
1. Main Configuration File Primary configuration (nginx.conf) is read first. /etc/nginx/nginx.conf
2. Specific Include Directives Files are inserted at the directive location. include conf.d/http;
3. Wildcard Inclusion Order Files matching a wildcard are processed in alphabetical order. include /etc/nginx/conf.d/*.conf;
4. Final Merging Collated configuration is parsed as a single configuration tree. Combined from all included files.

Table 1: Merge Order of NGINX Included Files

This structured merging does not only ensure a systematic build-up of the final configuration, but it also allows administrators to finely control which directives are applied where, enabling advanced configuration scenarios like dynamic load balancing and on-the-fly reconfiguration through the NGINX Plus API.

3.3 Visualization: NGINX Configuration Hierarchy Flowchart

The diagram below illustrates the hierarchical structure of an NGINX configuration, showing both the main file and the included directories:

flowchart TD  
    A["nginx.conf (Main Configuration)"] --> B["Global Directives (Main Context)"]  
    A --> C["HTTP Block"]  
    C --> D["Server Block 1"]  
    C --> E["Server Block 2"]  
    D --> F["Location Blocks in Server Block 1"]  
    E --> G["Location Blocks in Server Block 2"]  
    C --> H["Included Files (e.g., /etc/nginx/conf.d/*.conf)"]  
    H --> I["HTTP-specific configurations"]  
    H --> J["Stream configurations"]  
    H --> K["Feature-specific configurations"]

Figure 1: NGINX Configuration Hierarchy and Inclusion Flow

This flowchart highlights how configurations from included files merge into the overall setup by being incorporated at the appropriate context level defined by the primary file. This methodical structure is essential for troubleshooting and dynamic configuration management.


4. Directive Inheritance in NGINX Contexts

A defining feature of NGINX is directive inheritance. This feature allows directives specified in parent contexts (e.g., the http block) to automatically apply to child contexts (e.g., server and location blocks) unless they are explicitly overridden. This cascade effect reduces redundancy and ensures consistency across different parts of the configuration.

4.1 Basic Inheritance Rules

When an administrator places a directive in a parent context, all child contexts inherit that directive. For instance, if you define a directive in the http block, it is automatically applied to all nested server blocks and further to the location blocks, unless a child context redefines it.

Consider the following configuration example:

http {  
    add_header X-HTTP-LEVEL-HEADER 1;  # Defined in HTTP block  
    server {  
        listen 8080;  
        location / {  
            return 200 "OK";  
        }  
    }  
}  

In this example, the add_header directive defined in the http context is inherited by the server block and its nested location block. This inheritance helps ensure that global settings do not need to be repeated across multiple blocks, which streamlines configuration management.

4.2 Inheritance Exceptions: Overriding and Non-Inheritable Directives

Not all directives are treated equally in terms of inheritance. There are critical nuances to be aware of:

A clear understanding of these mechanisms is essential because misconfigurations can lead to unexpected behaviors in request processing and security settings.

4.3 Table: Comparison of Inheritance Behaviors

Below is a table that outlines the inheritance behavior of different directive types:

Directive Type Inheritance Behavior Example Directive Notes
Standard Directives Inherited by child contexts; can be overridden index, add_header Child value replaces parent if redefined
Array Directives Inherited as a complete set; redefining overrides all proxy_param, add_header Must restate all values to retain parent's settings
Command Directives Generally not inherited; require explicit definition try_files Must be defined at every relevant level

Table 2: Inheritance Behaviors of Various NGINX Directive Types

This table helps administrators quickly reference the behavior of different directives, ensuring proper configuration practices and reducing potential conflicts in complex configurations.


5. Overriding and Special Behaviors of Array and Command Directives

While basic inheritance provides a powerful mechanism to simplify configurations, the overriding rules for array and command directives add a layer of complexity. Here, we delve deeper into these nuances to ensure that configuration modifications do not unintentionally discard critical settings.

5.1 Array Directives and Their Overriding Rules

Array directives allow multiple values; however, when overridden in a child context, the entire array is replaced rather than merged with the parent's values. This behavior is critical when using directives such as proxy_param or additional header configurations.

For example, if the parent context defines:

server {  
    add_header X-PARENT-HEADER "ParentValue";  
}  

And a child context attempts to add its own header:

location /example {  
    add_header X-CHILD-HEADER "ChildValue";  
}  

The resulting output in the child context will only include X-CHILD-HEADER if no instruction is given to inherit the parent's header. If both are required, the child must explicitly include both directives:

location /example {  
    add_header X-PARENT-HEADER "ParentValue";  
    add_header X-CHILD-HEADER "ChildValue";  
}  

This explicit overriding mechanism prevents accidental aggregation of configuration values, ensuring that administrators must deliberately merge settings if that is the desired behavior.

5.2 Command Type Directives and Explicit Definition

Command directives do not allow for inheritance by default. Their behavior must be defined separately in each context where they apply. A common example is the try_files directive, which is used to determine how NGINX chooses a file to serve when a URL doesn’t map directly to a file. Since try_files is a command directive, it must be explicitly defined in each location block where its functionality is required, making configuration management more verbose but also clearer in intent.

5.3 Example: Overriding in Practice

Consider the following simplified configuration that demonstrates both inheritance and explicit overriding:

http {  
    # Global setting applies to all servers and locations by default  
    add_header X-Global "GlobalValue";  

    server {  
        listen 80;  
        server_name example.com;  

        # Inherits X-Global header  

        location / {  
            # Inherits X-Global header  
            return 200 "Hello, world!";  
        }  

        location /custom {  
            # Override global header; parent's X-Global is not automatically included  
            add_header X-Global "OverriddenValue";  
            add_header X-Custom "CustomValue";  
            return 200 "Custom location!";  
        }  
    }  
}  

In this configuration, the /custom location block explicitly overrides the inherited X-Global header value from the http block, demonstrating how child contexts can control their own behavior relative to global settings.


6. Practical Considerations and Best Practices

Understanding the configuration merge order and directive inheritance is critical for creating maintainable and scalable NGINX deployments. The following best practices have emerged from extensive use and community guidance:

6.1 Modularization of Configuration Files

6.2 Correct Use of Inheritance

6.3 Environment-Specific Configurations

6.4 Visualization: NGINX Configuration Merge and Inheritance Workflow

The following Mermaid diagram outlines the typical workflow of configuration merging and directive inheritance, highlighting the key decision points and outcomes in the configuration hierarchy:

flowchart TD  
    A["Start: nginx.conf"] --> B["Process Global Directives (Main Context)"]  
    B --> C["Enter HTTP Block"]  
    C --> D["Apply Global HTTP Settings"]  
    D --> E["Include Feature-specific Files (e.g., conf.d/*.conf)"]  
    E --> F["Files Processed Alphabetically"]  
    F --> G["Construct Combined HTTP Configuration"]  
    G --> H["Enter Server Block"]  
    H --> I["Server Directives Inherit from HTTP"]  
    I --> J["Enter Location Block"]  
    J --> K["Location Overrides if Defined"]  
    K --> L["Final Merged Configuration"]

Figure 2: Workflow of NGINX Configuration Merge and Inheritance

This diagram emphasizes how the configuration is built from the top-level file through included files and into nested contexts, clearly illustrating where overrides occur and how inheritance flows.

6.5 Testing and Reloading Configurations

Before applying configuration changes in production, it is essential to:


7. Conclusion

Understanding the merge order of NGINX configuration files along with the principles of directive inheritance is essential for designing reliable, maintainable, and scalable configurations. Key insights from this exploration include:

Main Findings:

Table 3: Summary of Best Practices for NGINX Configuration Management

Best Practice Description
Modular Configuration Split main configuration into smaller, feature-specific files
Use of Include Directives Use explicit and wildcard includes to control merge order and enable flexibility
Global Settings in HTTP Block Define commonly inherited directives at the highest appropriate level
Explicit Overrides for Arrays Restate inherited values when overriding array directives
Testing and Validation Regularly validate configuration syntax using nginx -t and testing in staging

By adhering to these principles and best practices, administrators can ensure that NGINX configurations not only meet performance and security requirements but also remain adaptable to evolving infrastructure needs. Detailed understanding of the configuration merge order and directive inheritance ultimately results in fewer errors, more predictable server behavior, and a robust platform capable of handling complex, high-availability deployments.


This comprehensive examination of NGINX’s configuration merge order and directive inheritance reveals the powerful yet nuanced mechanisms that underpin one of the most flexible web server solutions available today. Armed with this knowledge, system administrators and developers can design configurations tailored to their unique environments while maintaining clarity, consistency, and reliability.