9

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.

  • Do you mean like in this old post? I am not sure why you say it will not work: https://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf – number9 Jun 30 '22 at 14:33
  • Be a little more specific, can you give a code snippit? – number9 Jun 30 '22 at 14:33

1 Answers1

7

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.

rubynorails
  • 2,293
  • I think here is a more detailed response that cover interesting aspects. https://serverfault.com/questions/644892/nginx-set-variable-in-location/644898#644898 – LucianDex Sep 22 '23 at 09:11