Script: Assemble NGINX Configuration Files
merge-ngx-conf.pl is a perl script used to assemble a set of nginx configuration files for one site. It has a number of options. See the bitbucket page or the help documentation in the script itself.
In its simplest form, it’s called by issuing this command:
merge-ngx-conf.pl /path/sites-available/filename
The output is an assembled nginx configuration file with all the includes inserted. Using nginx.conf
and domain.conf
(or just domain.conf
depending on the options selected), the script iterates through the include directives in the files and inserts the text from the referenced file. The script handles wildcard masks and follows include directives down multiple levels (i.e. nested levels). It will also follow referenced files in directories external to the nginx configuration directory.
The script does not write to any file or alter existing files in any way so I would consider it safe to use on a production system. In fact, the script doesn’t even care if nginx is running. What it does care about is that the directory structure has, at a minimum:
/<some_directory>/nginx.conf
/<some_directory>/sites-available/<domain.conf>
Several options are included to limit the output to the domain configuration only, to vary the format of the output, to create a report, etc.
The use of nginx include directives makes for cleaner configuration files. Directives repeated numerous times across other files can be written once and then inserted wherever needed. It also unclutters the configuration file by allowing a particularly lengthy configuration section (like mime-types
) to be inserted. Complex configurations can contain many include directives, with some files referenced in the include directive containing include directives themselves. In essence, the include directives are “nested”. It then becomes increasingly difficult to pinpoint the source of a problem as you may have to go several “nest levels” down.
See nginx documentation for include directives for further information.
An example of nested files:
The file, nginx.conf
(calling file), contains the following include directive:
include example.conf;
The file, example.conf
(included file), also contains an include directive as well. example.conf
becomes the calling file.
include nest.conf; # This is a nested include directive
In this example, nest.conf
(included file) is inserted into example.conf
and then the altered example.conf
is inserted into nginx.conf
.
nginx.conf <-- example.conf <-- nest.conf