env
The 'env' directive allows you to define environment variables for the NGINX worker processes.
Description
The 'env' directive is used within the main context of the NGINX configuration file to specify environment variables that should be passed to the worker processes. This is particularly useful for setting configuration values that workers might need to access at runtime, such as API keys, database connection strings, or other settings that can vary by deployment environment. The syntax for the 'env' directive is straightforward; it takes one argument that specifies the name of the environment variable to be set. You can specify multiple 'env' directives to set several variables, as each directive applies independently.
In the underlying C code, the 'env' directive is processed during the NGINX startup phase. The specified variables are added to the environment variables of the NGINX worker processes, allowing them to be accessed like any standard environment variable. The 'env' directive is particularly useful in scenarios where you are deploying applications in containerized environments (like Docker) or orchestration systems (like Kubernetes), as it complements the deployment's configuration management practices, allowing easy modification of settings without changing the application code.
It's important to note that the values set by the 'env' directive will not persist across reloads of the NGINX process unless they are explicitly included again in the configuration file. Additionally, any changes made to environment variables via the system after starting NGINX will not take effect unless the NGINX process is restarted. This makes understanding how to utilize this directive effectively vital for maintaining configuration across deployments.
Config Example
env MY_DATABASE_USER; env MY_API_KEY;
Make sure to restart NGINX after adding or modifying 'env' directives for changes to take effect.
Environment variables must be referenced in the correct context to be accessible to the workers.