Precedence of nested Include and IncludeOptional directives
29.5K reputation · 15 Oct 2020, 08:38 UTC
Achieving a repeatable development environment in Apache HTTP Server often requires separating core logic from environment-specific overrides using Include and IncludeOptional directives. This modular approach allows for flexible configuration fragments across different deployment stages.
A challenge arises when managing complex directory structures where configuration files contain nested references or overlap. Since Apache processes directives linearly, the sequence of these statements determines the final state of module settings and server behavior.
When a main configuration file utilizes IncludeOptional to load a directory that contains further nested Include statements, the interaction between optional and mandatory loading can create uncertainty regarding the final precedence of conflicting directives.
- Does the linear processing order persist across nested include levels regardless of whether the parent was loaded via
IncludeOptional? - Which directive takes precedence when the same configuration parameter is defined in both a mandatory
Includefile and an optional override file loaded later in the sequence?
1 answer
1 question comment
Use comments to ask for clarification. Post a solution as an answer.
29,525 reputation · 15 Oct 2020, 17:44 UTC
While linear processing is the rule, it is important to clarify how Apache handles directory-based includes (e.g., IncludeOptional conf.d/*.conf). When a wildcard is used, the order of files loaded is determined by the filesystem's directory listing order, which is not guaranteed to be alphabetical or consistent across different operating systems.
If you rely on a specific override sequence—such as a base.conf being superseded by a custom.conf—using a glob pattern can introduce non-deterministic behavior. To ensure a predictable precedence in Apache 2.4.x, it is safer to:
- Explicitly list
Includestatements in the required order. - Use a naming convention (e.g.,
00-base.conf,99-override.conf) if the environment supports alphabetical sorting.
You can verify the actual load order by using the apachectl -S command or checking the error log for initialization sequences during a restart.