Updated March 10, 2023
Introduction to Nginx Environment Variables
Nginx environment variables are not supported in a configuration so it does not mean that there is not any solution to use the environment variables in nginx, we are using envsubst utility. As we know that nginx is a reverse proxy and web server, which become developers’ default choice. Using cloud computing while deploying the applications by using nginx is very efficient and easy.
Overview of Nginx Environment Variables
At the time of running applications on to the platform of hosting GCP, AWS or azure. To use the nginx server, we need to set port no from which we are listening. We are using the envsubst utility instead of environment variables because environment variables is not supporting into the nginx. The envsubst utility is part of gettext so we can say that it was available in docker images of nginx so we can say that applying solution is very easy. We need to apply template of envsubst configuration files for the container of the docker.
How to Use Nginx Environment Variables?
Nginx will be removing all the environment variables which was inherited from the parent process excepting the TZ variable. This directive into the environment variables is allowing preserved names of the inherited variables. The variable of TZ is available into the module of nginx http Perl module. We can update the configuration file for the docker container for the nginx. To use the nginx environment variables we need to install nginx and docker in our system.
Below steps shows how we can use environment variables as follows:
1. To use the nginx environment variables, first we need to install the nginx server in our system as follows.
Code:
apt-get install nginx
Output:
2. In ubuntu system the nginx server is not started after installing the nginx server by default to start the same we need to start by using service command. We can check the version of nginx server and supported modules as follows.
Code:
service nginx start
service nginx status
nginx –V
Output:
3. After starting the nginx server in this step we are installing the docker in ubuntu system by using apt-get command as follows.
Code:
apt-get install docker
Output:
4. After installing the docker in this step, we are configuring the docker-compose file as follows. In below file we can see that volume, ports and environment variable and then we are storing the same value into the nginx configuration file.
Code:
image: nginx
volumes:
- ./nginx_config:/etc/nginx/nginx_config
ports:
- "80"
environment:
- NGINX_HOST = localhost
- NGINX_PORT = 8080
Output:
5. As per the file of docker-compose, the nginx configuration file generated into the configuration of docker as follows.
Code:
server {
listen 8080;
listen [::]:80;
server_name www.nginx.com;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 400 404 504 501 /nginx.html;
location = /nginx.html {
root /usr/share/nginx/html;
}
}
Output:
Nginx Environment Variables Configurations Files
Basically nginx is http server which was performance oriented, as compare with the apache server it contains high stability and will consume less memory. We can deploy the nginx server with docker, the nginx docker server is related to the file name of the nginx configuration which was dynamically configured by using container starting. Basically file of nginx configuration is not supporting to the environment variables, we can use envsubst utility to use the feature of the configuration file of nginx.
Kernel and module is composed by using nginx. The design of the nginx is very concise and small, and also work which was completed is also very simple. We have only need to map the client request to the block of location to the configuration file location and instruction which was configured in the location from which we are starting the different modules to complete the work. Location into the nginx environment variables is an instruction for configuration of an nginx for the matching of URL. Basically, the nginx module is divided into two modules i.e. third-party modules and basic modules.
Below are the modules of nginx configuration files as follows:
1. Core modules: This modules contains the event module, http module and the mail module. This module is very important in the nginx configuration files.
2. Basic modules: This module contains the http fastcgi module, http access module, http rewrite module and http proxy module. This module we are using for the basic operations which was performed in our applications.
3. Third party modules: Notice module and access key module of http.
Envsubst replaces the variable of environment by using the value of specified tag into the file name as env.conf as follows.
Below is the configuration file as follows:
Code:
[nginx]
ip = ${ip}
port = ${port}
url = http://${ip}:${port}/index.html
Output:
While executing the export onto the environment variables the new file is generated name as env.new.conf. We can also specify the replacement only with specified variables.
Code:
[nginx]
ip = 192.168.92.125
port = 80
url = http://192.168.92.125:80/index.html
Output:
If suppose we want to replace only IP, then we can replace into the nginx env.conf files. In Linux testing, we can use only double quotation marks and single quotation marks, and the same is valid in the nginx env.conf file. We can apply the nginx profile in docker-compose.yml.
Below is the example of docker compose file as follows.
Code:
version : "3"
services :
nginx :
image : nginx:1.20.1-alpine
container_name : nginx_config
ports :
- 8080:8080
- 443:443
environment :
- NGINX_HOST = www.nginx.com
- NGINX_PORT = 8080
volumes :
- /root/nginx.temp:/etc/nginx/conf.d/nginx.temp
Command : /bin/sh -c "envsubst < /etc/nginx/conf.d/nginx.temp > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon on;'"
network_mode : bridge
restart : always
Output:
Conclusion
We are using the envsubst utility instead of environment variables because environment variables are not supporting into the nginx. We need to apply template of envsubst configuration files for the container of the docker. Basically nginx is http server which was more performance oriented, as compare with the apache server.
Recommended Articles
This is a guide to Nginx Environment Variables. Here we discuss the introduction, how to use Nginx environment variables? and configuration files. You may also have a look at the following articles to learn more –