Scenario / Questions
I am trying to optimize my nginx configs, so it would be possible to set one variable, and all location paths would update automatically. I have four lines in question:
server_name php.domain.com; root /srv/web/vhosts/php/web; error_log /srv/web/vhosts/php/logs/error.log; access_log /srv/web/vhosts/php/logs/access.log;
What I would like to achieve is to set one variable (in this case ‘php’) and include it to config.
set $variable "php"; server_name $variable.domain.com; root /srv/web/vhosts/$variable/web; error_log /srv/web/vhosts/$variable/logs/error.log; access_log /srv/web/vhosts/$variable/logs/access.log;
However it seams that nginx ignores variables in this config. Am I doing something wrong or it is not possible to use variable in location paths?
Find below all possible solutions or suggestions for the above questions..
Variables can’t be declared anywhere nor be used in any directive.
As the documentation of
set directive is :
Syntax: set $variable value; Default: — Context: server, location, if
The immediate consequence is that you can’t use custom variables in an
access_logcan contain variables with restrictions. Among them, the lack of buffering and the fact that the leading slash must not be declared in a variable.
error_logwon’t work with variables at all.
rootdirective can contains variables.
server_namedirective only allows strict
$hostnamevalue as a variable-like notation.
Disclaimer: This has been sourced from a third party syndicated feed through internet. We are not responsibility or liability for its dependability, trustworthiness, reliability and data of the text. We reserves the sole right to alter, delete or remove (without notice) the content in its absolute discretion for any reason whatsoever.
Source: nginx set variable in location