4

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
  • 1
    Why not just do nginx -t to check the syntax before restarting nginx? Commented Jun 4, 2013 at 19:17
  • 2
    To clarify, this is in order to ship config files which, if loaded in the absence of certain modules, won't make an end-user's nginx barf. To take the onus off of them to double-check settings when loading a provided config file. If certain module is missing, then no-op. Commented Feb 5, 2014 at 15:19

2 Answers 2

2

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.

Source: http://trac.nginx.org/nginx/ticket/126

1
  • 1
    this was my fear Commented Feb 5, 2014 at 15:19
3

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_module indicates a module (i.e. http_v2_module in this case) compiled statically into the binary;
  • --add-dynamic-module=/path/to/module_name indicates a dynamic module. it can be en(dis)abled without recompiling nginx, using the load_module module_name.so core directive.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.