Precedence of nested Include and IncludeOptional directives
0 reputation · 15 Oct 2020, 08:38 UTC
0 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.
IncludeOptional?Include file and an optional override file loaded later in the sequence?32775 reputation · 15 Oct 2020, 15:44 UTC
1. Does the linear processing order persist across nested include levels regardless of whether the parent was loaded via IncludeOptional?
Yes. Apache processes directives strictly in the order they appear in the configuration file. Nested Include or IncludeOptional directives are parsed after the outer directive, so the innermost file can override settings defined earlier. The fact that the parent was loaded via IncludeOptional only suppresses an error if the file is missing; it does not alter the ordering.
2. Which directive takes precedence when the same configuration parameter is defined in both a mandatory Include file and an optional override file loaded later in the sequence?
The later definition wins. When a parameter is set in multiple included files, the value from the file that is parsed last overrides earlier values. This applies to both Include and IncludeOptional files.
Include and IncludeOptional are parsed in the order they appear.VirtualHost, Directory, IfModule, etc.) follow the same ordering rule.AllowOverride may accumulate).IncludeOptional is handled.# base.conf
ErrorLog /var/log/apache2/error.log
# override.conf
ErrorLog /tmp/overridden.log
httpd.conf (or the appropriate virtual host file), add:
Include /etc/apache2/conf.d/base.conf
IncludeOptional /etc/apache2/conf.d/override.conf
sudo systemctl reload apache2
/tmp/overridden.log.
override.conf or comment its IncludeOptional line, reload, and verify that errors now go to /var/log/apache2/error.log.
If you observe unexpected behavior, confirm the following:
<VirtualHost>)?Providing one of these details will help narrow down any edge‑case interactions.
Use comments to ask for clarification. Post a solution as an answer.
32,775 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:
Include statements in the required order.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.