Is there a way to use a variable in nginx config?

In my nginx configuration file there're several identical values. Is there any way to specify a variable(s) or constant(s) for those in the beginning of a nginx config file, once, in order then to refer to it via a variable or constant?

If there's a way of achiving that via env. variables, it won't work for me. A variable will need to be set in an nginx file itself.

Answers 1

  • The syntax is set $variable_name variable_value;

    Then you can reference $variable_name in subsequent lines of the Nginx config.

    See more here.

    It is a feature of the rewrite module and not a feature of Nginx itself -- this means you must have the rewrite module enabled in order for this to work.

    If you install the nginx-extras package, this is enabled by default.

    You can read more about the ngx_http_rewrite_module here.

Related Questions