Dynamic default.conf of nginx in docker

Use envsubst to dynamically embed environment variables in the nginx configuration. envsubst is a lightweight template engine and it is also included in the official nginx:alpine image.

To install envsubst to your custom image:

alpine:

$ apk --no-cache add gettext

debian:

$ apt-get install gettext-base

Here is a simple example for how to use envsubst:

$ cat test.conf.template
hoge=$HOGE

$ docker run --rm \
    -v $(pwd)/test.conf.template:/tmp/test.conf.template \
    -e HOGE=aaa \
    nginx:alpine \
    /bin/sh -c "envsubst < /tmp/test.conf.template > /tmp/test.conf && cat /tmp/test.conf"
hoge=aaa

Note that if you want to use the $ symbol in the configuration file like nginx.conf, you need to specify the name of the environment variable to embed.

An example of dynamically embedding the environment variable SERVER_NAME in nginx.conf is as follows:

server {
    listen                          80;
    server_name                     ${SERVER_NAME};
    return 301                      https://${SERVER_NAME}$request_uri;
}

server {

    listen 443;
    server_name ${SERVER_NAME};

    ssl_certificate           /etc/ssl/private/server.crt;
    ssl_certificate_key       /etc/ssl/private/server.key;

    ssl on;

    access_log            /var/log/nginx/ghost.access.log;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;

      proxy_pass          http://xxx:2368;
      proxy_read_timeout  90;

      proxy_redirect      http://xxx:2368 https://${SERVER_NAME}:443;
    }
}

The arguments are somewhat complicated, so it is shown here in docker-compose.yml format:

version: '2'
services:
  nginx:
    image: nginx:alpine
    command: >
      /bin/sh -c
      "envsubst '
      $$SERVER_NAME
      '< /etc/nginx/nginx.conf.template
      > /etc/nginx/nginx.conf
      && nginx -g 'daemon off;'"
    volumes:
      - ./nginx.conf.template:/etc/nginx/nginx.conf.template
    ports:
      - 8080:80
    environment:
      SERVER_NAME: "test.example.com"

Tags:

Docker

Nginx