How nginx configuration files operate using environment variables

  • 2021-11-01 05:25:53
  • OfStack

Preface

Nginx is a performance-oriented HTTP server. Compared with Apache, lighttpd has the advantages of less memory and high stability.

Since docker of nginx needs to be deployed now, it is hoped that server_name in the nginx configuration file will be dynamically modified before starting the container.
However, the configuration file of nginx does not support the use of environment variables. I found a lot of solutions on the Internet and finally chose to use them envsubst Overwrite the nginx configuration file by.

Working principle

Nginx consists of a kernel and modules, in which the design of the kernel is very small and concise, and the work done is very simple. Only by looking up the configuration file, the client request is mapped to an location block (location is an instruction in Nginx configuration for URL matching), and each instruction configured in this location will start a different module to complete the corresponding work.

The modules of Nginx are divided into core module, basic module and third-party module in structure:

Core modules: HTTP module, EVENT module and MAIL module
Basic modules: HTTP Access module, HTTP FastCGI module, HTTP Proxy module and HTTP Rewrite module.
The third module: HTTP Upstream Request Hash module, Notice module and HTTP Access Key module.

Learn envsubst

envsubst is to replace the environment variable with the value of the specified tag in the file.
For example, there are the following files env.conf Which read as follows


[test]
ip = ${ip}
port = ${port}
url = http://${ip}:${port}/index.html
phone = ${phone}

When executing export ip=192.168.1.5 , export port=8081 , export phone=13522223334 Writes an environment variable.
Then execute envsubst < env.conf > env.new.conf You can generate the following env.new.conf


[test]
ip = 192.168.1.5
port = 8081
url = http://192.168.1.5:8081/index.html
phone = 13522223334

You can also specify that only some environment variables are replaced, source env.env && envsubst '$ip;$phone' < env.conf This only replaces the ip and phone variables.
The above only replaces the partial environment variable, in Linux test can only use the single quotation mark, uses the double quotation mark to be invalid, the delimiter has tried , . ; | All four are fine, and I guess there are more separators.

Apply nginx configuration file

docker-compose.yml The document is as follows


version: "3"
 
services:
  nginx:
    image: nginx:1.20.1-alpine
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    environment:
      - NGINX_HOST=www.janbar.com
      - NGINX_PORT=80
    volumes:
      - /root/janbar.temp:/etc/nginx/conf.d/janbar.temp
    command: /bin/sh -c "envsubst < /etc/nginx/conf.d/janbar.temp > /etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
    network_mode: bridge
    restart: always

env.conf0 The contents of the document are as follows


server {
    listen       ${NGINX_PORT};
    listen  [::]:${NGINX_PORT};
    server_name  ${NGINX_HOST};

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

In accordance with the above docker-compose.yml The configuration file eventually generates the configuration file inside the docker container as follows cat /etc/nginx/conf.d/default.conf


server {
    listen       80;
    listen  [::]:80;
    server_name  www.janbar.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Summarize

After the above-mentioned operation, the docker container internal configuration file of nginx can finally be updated by means of environment variables. Be crowned with success!

The above is the nginx configuration file using environment variables in detail, more information about the nginx environment variables please pay attention to other related articles on this site!


Related articles: