Module mod_macro

Module mod_macro supports directives for the IBM® HTTP Server for i Web server.

Summary

The mod_macro module provides macros within Apache HTTP Server runtime configuration files, to ease the process of creating numerous similar configuration blocks. When the server starts up, the macros are expanded using the provided parameters, and the result is processed as along with the rest of the configuration file. Macros are defined using <Macro> blocks, which contain the portion of your configuration that needs to be repeated, complete with variables for those parts that will need to be substituted. For example, you might use a macro to define a <VirtualHost> block, in order to define multiple similar virtual hosts:

<Macro VHost $name $domain>
<VirtualHost *:80>
    ServerName $domain
    ServerAlias www.$domain

    DocumentRoot /www/webserver/vhosts/$name
    ErrorLog /www/webserver/logs/$name.error_log
    CustomLog /www/webserver/logs/$name.access_log combined
</VirtualHost>
</Macro>

Macro names are case-insensitive, like HTTP Server configuration directives. However, variable names are case sensitive. You would then invoke this macro several times to create virtual hosts:

Use VHost example example.com
Use VHost myhost hostname.org
Use VHost apache apache.org

UndefMacro VHost

At server startup time, each of these Use invocations would be expanded into a full virtualhost, as described by the Macro definition.

The UndefMacro directive is used so that later macros using the same variable names don't result in conflicting definitions.

Parameter names should begin with a sign such as $, %, or @, so that they are clearly identifiable, and also in order to help deal with interactions with other directives, such as the core Define directive. Failure to do so will result in a warning. Nevertheless, you are encouraged to have a good knowledge of your entire server configuration in order to avoid reusing the same variables in different scopes, which can cause confusion.

Parameters prefixed with either $ or % are not escaped. Parameters prefixes with @ are escaped in quotes.

Avoid using a parameter which contains another parameter as a prefix, (For example, $win and $winter) as this may cause confusion at expression evaluation time. In the event of such confusion, the longest possible parameter name is used.

If you want to use a value within another string, it is useful to surround the parameter in braces, to avoid confusion:

<Macro DocRoot ${docroot}>
    DocumentRoot /www/${docroot}/htdocs
</Macro>

Examples:

Virtual Host Definition

A common usage of mod_macro is for the creation of dynamically-generated virtual hosts.

## Define a VHost Macro for repetitive configurations

<Macro VHost $host $port $dir>
  Listen $port
 <VirtualHost *:$port>
    ServerName $host
    DocumentRoot "$dir"

    # Public document root
    <Directory "$dir">
    Require all granted
    </Directory>
    # limit access to intranet subdir.
    <Directory "$dir/intranet">
      Require ip 10.0.0.0/8
    </Directory>
 </VirtualHost>
</Macro>

## Use of VHost with different arguments.

Use VHost www.apache.org 80 /www/webserver/vhosts/apache/htdocs

Use VHost example.org 8080 /www/webserver/vhosts/example/htdocs

Use VHost www.example.fr 1234 /www/webserver/vhosts/example.fr/htdocs

Removal of a macro definition

It's recommended that you undefine a macro once you've used it. This avoids confusion in a complex configuration file where there may be conflicts in variable names.

<Macro DirGroup $dir $group>
  <Directory "$dir">
    Require group $group
  </Directory>
</Macro>

Use DirGroup /www/webserver/private private

Use DirGroup /www/webserver/server admin

UndefMacro DirGroup

Directives

<Macro>

Module: mod_macro
Syntax: <Macro name [par1 .. parN]> ... </Macro>
Default: none
Context: server config, virtual host, directory
Override: none
Origin: Apache
Examples: See below

The Macro directive controls the definition of a macro within the server runtime configuration files. The first argument is the name of the macro. Other arguments are parameters to the macro. It is good practice to prefix parameter names with any of '$%@', and not macro names with such characters.

For examples:

<Macro LocalAccessPolicy>
    Require ip 10.2.16.0/24
</Macro>

<Macro RestrictedAccessPolicy $ipnumbers>
    Require ip $ipnumbers
</Macro>

UndefMacro

Module: mod_macro
Syntax: UndefMacro name
Default: none
Context: server config, virtual host, directory
Override: none
Origin: Apache
Examples: See below

The UndefMacro directive undefines a macro which has been defined before hand.

For examples:

<Macro LocalAccessPolicy>
    Require ip 10.2.16.0/24
</Macro>

<Macro RestrictedAccessPolicy $ipnumbers>
    Require ip $ipnumbers
</Macro>

UndefMacro LocalAccessPolicy
UndefMacro RestrictedAccessPolicy

Use

Module: mod_macro
Syntax: Use name [value1 ... valueN]
Default: none
Context: server config, virtual host, directory
Override: none
Origin: Apache
Examples: See below

The Use directive controls the use of a macro. The specified macro is expanded. It must be given the same number of arguments as in the macro definition. The provided values are associated to their corresponding initial parameters and are substituted before processing.

For examples:

<Macro LocalAccessPolicy>
    Require ip 10.2.16.0/24
</Macro>

<Macro RestrictedAccessPolicy $ipnumbers>
    Require ip $ipnumbers
</Macro>

Use LocalAccessPolicy
...
Use RestrictedAccessPolicy "192.54.172.0/24 192.54.148.0/24"
is equivalent, with the macros defined above, to:
Require ip 10.2.16.0/24
...
Require ip 192.54.172.0/24 192.54.148.0/24