I have a question for all. Modules such as mod_dir, mod_alias, mod_rewrite… how does Apache determine what order to process them?

 

Apache Modules priority


 

Here’s where the documentation explains calling order: httpd.apache.org/docs/2.4/developer/hooks.html#hooking-order 

When you install mod_info module, than mod_info provides a much more readable way to see the priority of each module for each hook:

 

So, all statements for mod_rewrite are executed first, than is executed mod_proxy, than mod_alias and than core of apache web server in the same configuration context.

 

Order of Processing of mod_alias


 

Aliases and Redirects occurring in different contexts are processed like other directives according to standard merging rules. But when multiple Aliases or Redirects occur in the same context (for example, in the same <VirtualHost> section) they are processed in a particular order.

First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.

For this reason, when two or more of these directives apply to the same sub-path, you must list the most specific path first in order for all the directives to have an effect. For example, the following configuration will work as expected:

But if the above two directives were reversed in order, the /foo alias would always match before the /foo/bar alias, so the latter directive would be ignored.

When the Alias, ScriptAlias and Redirect directives are used within a <Location> or <LocationMatch> section, these directives will take precedence over any globally defined Alias, ScriptAlias and Redirect directives.

 

First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.

 

How the sections are merged


 

The configuration sections are applied in a very particular order. Since this can have important effects on how configuration directives are interpreted, it is important to understand how this works.

The order of merging is:

  1. <Directory> (except regular expressions) and .htaccess done simultaneously (with .htaccess, if allowed, overriding <Directory>)
  2. <DirectoryMatch> (and <Directory "~">)
  3. <Files> and <FilesMatch> done simultaneously
  4. <Location> and <LocationMatch> done simultaneously
  5. <If>

Some important remarks:

  • Apart from <Directory>, within each group the sections are processed in the order they appear in the configuration files. For example, a request for /foo will match <Location "/foo/bar"> and <Location "/foo"> (group 4 in this case): both sections will be evaluated but in the order they appear in the configuration files.
  • <Directory> (group 1 above) is processed in the order shortest directory component to longest. For example, <Directory "/var/web/dir"> will be processed before <Directory "/var/web/dir/subdir">.
  • If multiple <Directory> sections apply to the same directory they are processed in the configuration file order.
  • Configurations included via the Include directive will be treated as if they were inside the including file at the location of the Include directive.
  • Sections inside <VirtualHost> sections are applied after the corresponding sections outside the virtual host definition. This allows virtual hosts to override the main server configuration.
  • When the request is served by mod_proxy, the <Proxy> container takes the place of the <Directory> container in the processing order.

 

Technical Note

There is actually a <Location></LocationMatch> sequence performed just before the name translation phase (where Aliases and DocumentRoots are used to map URLs to filenames). The results of this sequence are completely thrown away after the translation has completed.

 

Relationship between modules and configuration sections:


 

One question that often arises after reading how configuration sections are merged is related to how and when directives of specific modules like mod_rewrite are processed. The answer is not trivial and needs a bit of background. Each httpd module manages its own configuration, and each of its directives in httpd.conf specify one piece of configuration in a particular context. httpd does not execute a command as it is read.

At runtime, the core of httpd iterates over the defined configuration sections in the order described above to determine which ones apply to the current request. When the first section matches, it is considered the current configuration for this request. If a subsequent section matches too, then each module with a directive in either of the sections is given a chance to merge its configuration between the two sections. The result is a third configuration, and the process goes on until all the configuration sections are evaluated.

After the above step, the “real” processing of the HTTP request begins: each module has a chance to run and perform whatever tasks they like. They can retrieve their own final merged configuration from the core of the httpd to determine how they should act.

An example can help to visualize the whole process. The following configuration uses the Header directive of mod_headers to set a specific HTTP header. What value will httpd set in the CustomHeaderName header for a request to /example/index.html ?

 

  • Directory “/” matches and an initial configuration to set the CustomHeaderName header with the value one is created.
  • Directory “/example” matches, and since mod_headers specifies in its code to override in case of a merge, a new configuration is created to set the CustomHeaderName header with the value two.
  • FilesMatch “.*” matches and another merge opportunity arises, causing the CustomHeaderName header to be set with the value three.
  • Eventually during the next steps of the HTTP request processing mod_headers will be called and it will receive the configuration to set the CustomHeaderName header with the value three. mod_headers normally uses this configuration to perfom its job, namely setting the foo header. This does not mean that a module can’t perform a more complex action like discarding directives because not needed or deprecated, etc..

This is true for .htaccess too since they have the same priority as Directory in the merge order. The important concept to understand is that configuration sections like Directory and FilesMatch are not comparable to module specific directives like Header or RewriteRule because they operate on different levels.

 

Some useful examples:

Below is an artificial example to show the order of merging. Assuming they all apply to the request, the directives in this example will be applied in the order A > B > C > D > E.

 

For a more concrete example, consider the following. Regardless of any access restrictions placed in <Directory> sections, the <Location> section will be evaluated last and will allow unrestricted access to the server. In other words, order of merging is important, so be careful!

 

Related Post

Apache server – Terms Used to Describe Direc... Have you sometime a question what is url, url-path, file-path, regex, mime-type and more? The answer is in this article. We describe a basic terms use...
Rewrite uri to lowercase in nginx webserver Rewrite uri to lowercase in nginx webserver   We want make our uri case insensitive. What it is? When I call uri like https://manpages....

Pin It on Pinterest