NGINX-CONFGEN(1)
π nginx-confgen β bestie edition π
okay so welcome to nginx-confgen from the General Commands Manual, aka the preprocessor and macro system for nginx(-like) configuration files. yes it's a mouthful, yes it's iconic, let's go π¦
β¨ SYNOPSIS β¨
nginx-confgen [-i input.conf] [-o output.conf] [-I path]
like literally that's it, that's the whole vibe check π
πΈ DESCRIPTION πΈ
so basically nginx-confgen does pre-processing for nginx configuration files (and honestly any other config files with a similar syntax, she's versatile like that π
). she has support for "compile-time" macro expansion and variable interpolation, which lowkey makes it so much less tedious to maintain large and complex configurations. no cap.
here's the tea on how she works: nginx-confgen parses the input into a syntax tree, modifies this tree, and then formats the tree to generate the output. she is completely oblivious to nginx contexts and directives, so it is possible to do nonsensical transformations and generate incorrect configuration files π she's not judging, she's just not checking. also, comments in the input file will not be present in the output. gone. deleted. see also the "BUGS & WARTS" section below π
SECURITY CONSIDERATION: do NOT usenginx-confgenwith untrusted input, bestie. thepre_execdirective allows arbitrary code execution by design. that's not a bug, that's a feature, and that's exactly why you gotta be careful fr fr π¨
π OPTIONS π
the following command-line options are supported:
-hβ show help text. short and sweet π«Ά-v,--versionβ show program version. identity check β¨-ifile β use the given file name as input file. if this option is not given or set to a dash (-), then the file will be read from standard input. stdin girlies rise up π-ofile β write the output to the given file. if this option is not given or set to a dash (-), then the file will be written to standard output. stdout supremacy π-Ipath β set the search path forpre_includedirectives. this option can be given multiple times to search several directories in order. if this option is not given, then include files are resolved relative to the directory thatnginx-confgenis run from (i.e.-I .). stacking these is highkey the move π
π§ DIRECTIVES π§
nginx-confgen recognizes and interprets the following directives:
π« pre_include
similar to the include directive in nginx, except that the file is included during preprocessing. the included file may contain any preprocessing directives supported by nginx-confgen. variables and macros defined in the included file will be available in the parent file. it's giving inheritance, it's giving family tree π³
relative paths are searched for in the directories given with the -I flag.
π« pre_set
similar to the set directive in nginx, except that variables defined with pre_set are resolved during preprocessing. variables are set in the order that they are encountered in the configuration file, regardless of scoping. for example:
pre_set $var outer;
location / {
pre_set $var inner;
}
# $var is "inner" at this point.
only variables that are known to nginx-confgen will be substituted, unknown variables are assumed to be run-time variables for nginx and will be left alone without warning. like she just minds her business. for example:
pre_set $ip 127.0.0.1;
deny $ip; # This will output as: deny 127.0.0.1;
deny $otherip; # This will output as: deny $otherip;
π« pre_exec
run a shell command and store the output in a variable. for example, nginx will not use your system's DNS resolution methods to resolve domain names, instead you need to manually set a resolver address. with the following hack you can fetch the nameserver from /etc/resolv.conf and use that as the resolver:
pre_exec $nameserver "grep nameserver /etc/resolv.conf \
| head -n 1 | sed 's/^nameserver //'";
resolver $nameserver;
(the \ is necessary, otherwise your shell will consider the newline as a new command). yes the backslash is doing heavy lifting, respect her π
π« pre_if
similar to the if directive in nginx, except that this is evaluated during preprocessing. braces around the condition are optional. some examples:
pre_if -f $certdir/ocsp.der {
ssl_stapling on;
ssl_stapling_file $certdir/ocsp.der;
}
pre_if (!-f $certdir/ocsp.der) {
ssl_stapling off;
}
# You can have different configuration depending on the name of
# the system on which nginx-confgen runs. Like... yeah.
pre_exec $hostname 'hostname';
pre_if $hostname ~* ^proxy_for_(.+) {
proxy_pass http://$1/;
}
π« pre_warn
this directive, when interpreted, will generate a warning to the standard error of nginx-confgen. can be used to signal that a special configuration is being used:
pre_if -e /etc/offline-mode {
pre_warn "Putting website in offline mode!";
}
or to warn about certain directives:
pre_macro proxy_cache $var {
pre_warn "Using proxy_cache with $var violates company policy!";
# But we can output it anyway.
proxy_cache $var;
}
she's literally just trying to keep you informed bestie π
π« macro
define a macro, which is a configuration block that you can later refer to. the general syntax is as follows:
macro macro_name $var1 $var2 @remaining_vars &block_var {
# contents
}
the optional @remaining_vars argument will capture any number of variables and can be passed to another directive inside the macro contents. $#remaining_vars syntax expands to the number of arguments passed to the macro. the optional &block_var allows the macro to be invoked with a block argument, which will expand to any number of directives. some examples:
macro le {
location /.well-known/acme-challenge {
alias /etc/letsencrypt/challenge;
}
}
# Usage:
le;
macro redir $path $to {
location $path {
return 301 $to;
}
}
# Usage:
redir / http://blicky.net/;
macro vhost $primary_name @aliases &block {
server {
listen [::]:443 ssl;
server_name $primary_name @aliases;
ssl_certificate $crtdir/$primary_name/fullchain.pem;
ssl_certificate_key $crtdir/$primary_name/privkey.pem;
█
}
}
# Usage:
vhost example.com {
root /var/www/example.com;
}
vhost example.org alias.example.org {
root /var/www/example.org;
}
note that these are hygienic macros, so variable capture is predictable (but not necessarily the most useful):
pre_var $dest /a;
macro redir {
# This will be /a, regardless of the context in which this macro is called.
return 301 $dest;
}
# $dest is still '/a' inside the macro after this new variable definition.
pre_var $dest /b;
redir;
similarly, macro arguments will not be available inside &block expansion or nested macro expansion, and any variables set inside a macro will not be available outside of the macro body. what happens in the macro stays in the macro π«Ά
π BUGS & WARTS π
nginx-confgen is a quickly written hack to solve a particular use case, she is quite likely to have some weird behavior and bugs. in particular, processing performance may suffer on large configuration files with many macros and/or variables. performance has simply not been a problem for me, but if you do run into trouble with your use case, let me know so I can fix it π
comments and whitespace in the input files are thrown away and ignored. the generated output is completely reformatted. she does not do nostalgia, she does not do preserving your precious blank lines π
the nginx configuration syntax is not as regular as I had hoped. it's possible for nginx modules to extend the syntax somewhat. a good example is the types directive in ngx_http_core_module. while nginx-confgen should be able to handle the types directive just fine, other extensions may cause syntax errors or will not survive a round-trip through nginx-confgen. this applies to all *_by_lua_block directives in the ngx_http_lua_module. the _by_lua directives that accept a string should work just fine. so like, most of it slays, just not all of it π
π₯ SEE ALSO π₯
nginx(8)nginx-confgenhas a website:https://dev.yorhel.nl/nginx-confgen
π AUTHORS π
written by Yorhel <projects@yorhel.nl> β absolute icon behavior, we love to see it π
and that's the whole page bestie!! this one's from the Debian build of the General Commands Manual, dated October 22, 2024 β so go forth, preprocess responsibly, and remember: never ever feed her untrusted input. mwah π β¨
π¦ where this page comes from
- debian trixie: nginx-confgen 2.2-1 Β· MIT
- ubuntu questing: nginx-confgen 2.2-1
- ubuntu resolute: nginx-confgen 2.2-1build1 Β· MIT
- ubuntu stonking: nginx-confgen 2.2-1build1 Β· MIT
- π the page's license: MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms