3
2

More than 1 year has passed since last update.

NginxのDockerで環境変数を使ってdefault.confを変更する

Last updated at Posted at 2022-01-22

NginxのDocker Imageでは/etc/nginx/templatesにテンプレートファイル(サフィックス.template)を置くと自動でenvsubstで環境変数が置換処理されたファイルが/etc/nginx/conf.dに生成される機能がついています。
これを使うと、default.confの設定変更をコンテナの環境変数で行うことが可能です。

下記のようにdocker-composeで/etc/nginx/template/default.conf.templateを配置した場合、envsubstで処理された/etc/nginx/conf.d/default.confが生成されます。

version: "3"
services:
  nginx:
    image: nginx:1.21.4
    volumes:
      - ./templates:/etc/nginx/templates
    ports:
      - "8080:80"
    environment:
      - NGINX_HOST=localhost
      - NGINX_PORT=80
      - PHP_HOST=php

/etc/nginx/templates/default.conf.template

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

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root            /var/www;
        fastcgi_index   index.php;
        fastcgi_pass    ${PHP_HOST}:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

/etc/nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root            /var/www;
        fastcgi_index   index.php;
        fastcgi_pass    php:9000;
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

通常のenvsubstだと$document_rootなどのdefault.confで使われる変数も置換処理されて消えてしまいますが、この機能を使うと設定した環境変数だけが置換されます。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2