What I want to do is guard against configuration segments with invalid directives being run in an nginx that doesn't have the proper module installed. Something like the IfModule directive in Apache (http://httpd.apache.org/docs/current/mod/core.html#ifmodule). Does anything similar exist in nginx?
2 Answers
Apache's <IfModule> applies a set of directives if the specific module is loaded.
Since Nginx does not support dynamic module loading, this feature is not available.
- 1
The availability of a feature cannot be determined from within the configuration file as far as I can tell.
If one wishes to test if a change to the configuration would work under the current settings, one can use the configtest command: nginx -t (or /etc/init.d/nginx configtest) before changes go live. It's not ideal.
nginx supports dynamic module loading since version 1.9.11 (feb 2016 announcement). Still, one cannot provide alternate configs based on presence or absence of modules.
One shouldn't conflate the ability to load modules dynamically or statically, with the ability to test whether the modules in question are enabled, using directives in a config file. Regardless of the way a module is added to the binary, being able to test whether modular features are available would be a reasonable feature to support. That feature just isn't there yet, with the exception perhaps of a few modules which have perceptible side effects in the config (adding a custom response header for instance would be visible to a later step in the request processing).
If "scripting glue" is an option in the deployment to test for features, on the commandline, one can do: nginx -V to see the list of known modules at the time of compilation. The output will have a mix of --with-* and --add-dynamic-module flags.
--with-http_v2_moduleindicates a module (i.e. http_v2_module in this case) compiled statically into the binary;--add-dynamic-module=/path/to/module_nameindicates a dynamic module. it can be en(dis)abled without recompiling nginx, using theload_module module_name.socore directive.
nginx -tto check the syntax before restarting nginx?