cgi_set_var
The 'cgi_set_var' directive sets environment variables for CGI scripts.
Description
The 'cgi_set_var' directive is used to define and set environment variables in the context of CGI scripts executed by NGINX. This directive allows users to specify variables using a name-value pair format, where the first argument is the name of the environment variable and the second argument is the value to assign to it. This is particularly useful for configuring CGI scripts to behave differently based on the environment settings, such as modifying script behavior or providing necessary configuration parameters.
When the 'cgi_set_var' directive is invoked within a location or server block, the specified variables are passed to the CGI script as part of the execution environment. This enables greater flexibility and control over how scripts receive input data and configuration information. For example, you could set variables that determine log levels, paths, or other runtime parameters that scripts may need to function correctly. The directive is executed for every request that matches the defined context, ensuring that the environment is tailored for each CGI request.
It is important to note that the value can include references to other variables or can be hardcoded strings. If the value is dynamic (i.e., referencing other NGINX variables), it should be defined correctly to ensure it resolves as intended at request processing time. Users should also ensure that variable names are unique and do not conflict with existing CGI environment variables unless intentionally overridden.
Config Example
location /cgi-bin/ {
cgi_set_var SCRIPT_ENV 'prod';
cgi_set_var LOG_LEVEL 'info';
cgi_pass /usr/bin/php-cgi;
}Ensure variable names do not conflict with reserved CGI variables, which may lead to unexpected behaviors.
Quotes around values are required if they contain spaces or special characters.
Overriding existing CGI environment variables may have unintended consequences for existing scripts.