从docker-compose替换NGINX配置中的环境变量 [英] Substitute environment variables in NGINX config from docker-compose

查看:1390
本文介绍了从docker-compose替换NGINX配置中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在通过docker-compose配置的Docker容器中启动NGINX服务器。然而,我要注意的是,我想在http节中,特别是在上游块中替换一个环境变量。



能够运行该功能真是太棒了,因为我还有其他几个容器都是通过环境变量配置的,并且我有大约5个环境需要在任何给定的时间。我尝试使用 envsubst(由NGINX官方文档建议),perl_set和set_by_lua,但是它们似乎都不起作用。



下面是NGINX配置,就像我最近一次试用后一样

  user nginx; 
worker_processes 1;
env NGINXPROXY;

load_module modules / ngx_http_perl_module.so;

error_log /var/log/nginx/error.log警告;
pid /var/run/nginx.pid;


个事件{
worker_connections 1024;
}

http {
perl_set $ nginxproxy’sub {return $ ENV { NGINXPROXY}; }’;

上游api上游{
服务器$ {nginxproxy};
}

include /etc/nginx/mime.types;
default_type应用程序/字节流;

log_format main'$ remote_addr-$ remote_user [$ time_local] $ request'
'$ status $ body_bytes_sent $ http_referer'
' $ http_user_agent $ http_x_forwarded_for';

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

sendfile关闭;
#tcp_nopush on;

keepalive_timeout 65;

#gzip压缩;

包括/etc/nginx/conf.d/*.conf;
}

下面是NGINX dockerfile

 #构建阶段
FROM节点:最新
WORKDIR / app
COPY ./ / app
RUN npm安装
运行npm运行构建

#生产阶段
FROM nginx:1.17.0-perl
COPY --from = 0 / app / dist / usr / share / nginx / html
运行apt-get update&& apt-get install -y gettext-base
运行rm /etc/nginx/conf.d/default.conf
运行rm /etc/nginx/nginx.conf
COPY default.conf / etc / nginx / conf.d
复制nginx.conf / etc / nginx
RUN mkdir / certs
EXPOSE 80443
CMD [ nginx, -g,守护程序关闭;]

以下是docker-compose.yml部分NGINX服务器(名称和IP更改)。在我的疑难解答中,有意将envsubst命令注释掉了。

 前端:
环境:
-NGINXPROXY = 172.31.67.100:9300
构建:http:// gitaccount:password@gitserver.com/group/front-end.git#develop
container_name:qa_front_end
图片: qa前端
重新启动:始终
网络:
qa_network:
ipv4_address:172.28.0.215
端口:
- 9080:80
#命令:/ bin / bash -c envsubst'$$ NGINXPROXY'< /etc/nginx/nginx.conf> /etc/nginx/nginx.conf&& nginx -g'守护进程关闭; '

当我在上游块中引用$ nginxproxy变量时,似乎正在发生(右在服务器之后),我得到的输出看起来像是在引用字符串文字 $ nginxproxy,而不是替换变量的值。

  qa3_front_end | 2019/06/18 12:35:36 [emerg] 1#1:在/etc/nginx/nginx.conf的上游 $ {nginx_upstream}中找不到主机:19 
qa3_front_end | nginx:在/etc/nginx/nginx.conf的上游 $ {nginx_upstream}中找不到[emerg]主机:19
qa3_front_end退出,代码为1

当我尝试使用envsubst时,出现一个错误,听起来像是命令弄乱了nginx.conf文件的格式

  qa3_front_end | 2019/06/18 12:49:02 [emerg] 1#1:配置
中没有事件部分qa3_front_end | nginx:[emerg]配置中没有事件部分
qa3_front_end退出,代码为1

我很困,所以在此先感谢您的帮助。

解决方案

因此,在对此问题进行了一些努力之后,我设法使它的工作方式与bellackn提供的答案类似。如果有人需要参考完整的解决方案,我将在此处发布我的确切解决方案。



Step1:编写您的nginx.conf或default.conf的方式通常写出来。将文件另存为 nginx.conf.template或 default.conf.template,具体取决于您尝试将变量替换为哪个变量。

  user nginx; 
worker_processes 1;

error_log /var/log/nginx/error.log警告;
pid /var/run/nginx.pid;


个事件{
worker_connections 1024;
}

http {
上游api-上游{
服务器192.168.25.254;
}

include /etc/nginx/mime.types;
default_type应用程序/字节流;

log_format main'$ remote_addr-$ remote_user [$ time_local] $ request'
'$ status $ body_bytes_sent $ http_referer'
' $ http_user_agent $ http_x_forwarded_for';

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

sendfile关闭;
#tcp_nopush on;

keepalive_timeout 65;

#gzip压缩;

包括/etc/nginx/conf.d/*.conf;
}

Step2:用$ {VARNAME}格式替换变量对于要用环境变量替换的任何值:

  user nginx; 
worker_processes 1;

error_log /var/log/nginx/error.log警告;
pid /var/run/nginx.pid;


个事件{
worker_connections 1024;
}

http {
上游api上游{
服务器$ {SERVER_NAME};
}

include /etc/nginx/mime.types;
default_type应用程序/字节流;

log_format main'$ remote_addr-$ remote_user [$ time_local] $ request'
'$ status $ body_bytes_sent $ http_referer'
' $ http_user_agent $ http_x_forwarded_for';

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

sendfile关闭;
#tcp_nopush on;

keepalive_timeout 65;

#gzip压缩;

包括/etc/nginx/conf.d/*.conf;
}

步骤3:在docker-file中,复制您的nginx配置文件(您的nginx.conf.template或default.conf.template)放入容器中的适当位置:

 #构建阶段
FROM节点:最新
WORKDIR / app
COPY ./ / app
RUN npm install
RUN npm run build

#生产阶段
FROM nginx:1.17.0-perl
COPY --from = 0 / app / dist / usr / share / nginx / html
运行apt-get update&& apt-get install -y gettext-base
运行rm /etc/nginx/conf.d/default.conf
运行rm /etc/nginx/nginx.conf
#---- -------------------------------#
| COPY default.conf /etc/nginx/conf.d |
|复制nginx.conf.template / etc / nginx |
#-----------------------------------#
RUN mkdir / certs
EXPOSE 80443
CMD [ nginx,-g, daemon off;]

第4步:使用环境部分标签在docker-compos.yml文件中设置环境变量。确保您的环境变量名称与您在nginx配置文件中选择的任何变量名称匹配。在泊坞窗容器中使用 envsubt命令将变量值替换为nginx.conf.template中的变量,然后将输出写入正确位置的名为nginx.conf的文件中。可以通过使用命令部分标签在docker-compose.yml文件中完成:

 版本: 2.0 
服务:
前端:
环境:
-SERVER_NAME = 172.31.67.100:9100
构建:http:// git-account:git-password @ git-server.com/project-group/repository-name.git#branch-ame
container_name:qa_front_end
图片:qa-front-end-vue
重新启动:始终
网络:
qa_network:
ipv4_address:172.28.0.215
端口:
- 9080:80
命令:>
/ bin / sh -c
envsubst'
$$ {SERVER_NAME}
'< /etc/nginx/nginx.conf.template
> /等/nginx/nginx.conf
&& nginx -g'daemon off;'

第5步:使用docker-compose up(使用所需的任何其他开关)运行堆栈,您的nginx服务器现在应该以docker-compose.yml的 environment部分中提供的任何值开头。 >

如上面的解决方案中所述,您也可以定义自己的入口点,但是该解决方案也被证明可以很好地工作,并将所有内容保存在单个配置文件中,我有能力直接从git运行一堆服务,只需要一个docker-compose.yml文件。



非常感谢所有花时间准备的人通过这一点,贝拉肯花时间帮助我解决问题。


I am trying to start an NGINX server within a docker container configured through docker-compose. The catch is, however, that I would like to substitute an environment variable inside of the http section, specifically within the "upstream" block.

It would be awesome to have this working, because I have several other containers that are all configured through environment variables, and I have about 5 environments that need to be running at any given time. I have tried using "envsubst" (as suggested by the official NGINX docs), perl_set, and set_by_lua, however none of them appear to be working.

Below is the NGINX config, as it is after my most recent trial

user  nginx;
worker_processes  1;
env NGINXPROXY;

load_module modules/ngx_http_perl_module.so;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    perl_set $nginxproxy 'sub { return $ENV{"NGINXPROXY"}; }';

    upstream api-upstream {
        server ${nginxproxy};
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        off;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Below is the NGINX dockerfile

# build stage
FROM node:latest
WORKDIR /app
COPY ./ /app
RUN npm install
RUN npm run build

# production stage
FROM nginx:1.17.0-perl
COPY --from=0 /app/dist /usr/share/nginx/html
RUN apt-get update && apt-get install -y gettext-base
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d
COPY nginx.conf /etc/nginx
RUN mkdir /certs
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]

Below is the section of the docker-compose.yml for the NGINX server (with names and IPs changed). The envsubst command is intentionally commented out at this point in my troubleshooting.

front-end:
        environment:
            - NGINXPROXY=172.31.67.100:9300
        build: http://gitaccount:password@gitserver.com/group/front-end.git#develop
        container_name: qa_front_end
        image: qa-front-end
        restart: always
        networks:
            qa_network:
                ipv4_address: 172.28.0.215
        ports:
            - "9080:80"
        # command: /bin/bash -c "envsubst '$$NGINXPROXY' < /etc/nginx/nginx.conf > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"

What appears to be happening is when I reference the $nginxproxy variable in the upstream block (right after "server"), I get output that makes it look like it's referencing the string literal "$nginxproxy" rather than substituting the value of the variable.

qa3_front_end       | 2019/06/18 12:35:36 [emerg] 1#1: host not found in upstream "${nginx_upstream}" in /etc/nginx/nginx.conf:19
qa3_front_end       | nginx: [emerg] host not found in upstream "${nginx_upstream}" in /etc/nginx/nginx.conf:19
qa3_front_end exited with code 1

When I attempt to use envsubst, I get an error that makes it sound like the command messed with the format of the nginx.conf file

qa3_front_end       | 2019/06/18 12:49:02 [emerg] 1#1: no "events" section in configuration
qa3_front_end       | nginx: [emerg] no "events" section in configuration
qa3_front_end exited with code 1

I'm pretty stuck, so thanks in advance for your help.

解决方案

So after some wrestling with this issue, I managed to get it working similarly to the answer provided by bellackn. I am going to post my exact solution here, in case anybody else needs to reference a complete solution.

Step1: Write your nginx.conf or default.conf how you would normally write it. Save the file as "nginx.conf.template", or "default.conf.template" depending on which you are trying to substitute variables into.

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    upstream api-upstream {
        server 192.168.25.254;
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        off;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Step2: Substitute a variable in the format ${VARNAME} for whatever value(s) you want to replace with an environment variable:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    upstream api-upstream {
        server ${SERVER_NAME};
    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile        off;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

Step 3: In your docker-file, copy your nginx configuration files (your nginx.conf.template or default.conf.template) into your container at the appropriate location:

# build stage
FROM node:latest
WORKDIR /app
COPY ./ /app
RUN npm install
RUN npm run build

# production stage
FROM nginx:1.17.0-perl
COPY --from=0 /app/dist /usr/share/nginx/html
RUN apt-get update && apt-get install -y gettext-base
RUN rm /etc/nginx/conf.d/default.conf
RUN rm /etc/nginx/nginx.conf
#-----------------------------------#
|COPY default.conf /etc/nginx/conf.d|
|COPY nginx.conf.template /etc/nginx|
#-----------------------------------#
RUN mkdir /certs
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]

Step 4: Set your environment variable in your docker-compos.yml file using the "environment" section label. Make sure your environment variable name matches whatever variable name you chose within your nginx config file. Use the "envsubt" command within your docker container to substitute your variable values in for your variables within your nginx.conf.template, and write the output to a file named nginx.conf in the correct location. This can be done within the docker-compose.yml file by using the "command" section label:

version: '2.0'
services:
    front-end:
        environment:
            - SERVER_NAME=172.31.67.100:9100
        build: http://git-account:git-password@git-server.com/project-group/repository-name.git#branch-ame
        container_name: qa_front_end
        image: qa-front-end-vue
        restart: always
        networks:
            qa_network:
                ipv4_address: 172.28.0.215
        ports:
            - "9080:80"
        command: >
            /bin/sh -c
            "envsubst '
            $${SERVER_NAME}
            '< /etc/nginx/nginx.conf.template
            > /etc/nginx/nginx.conf
            && nginx -g 'daemon off;'"

Step 5: Run your stack with docker-compose up (with whatever additional switches you need) and your nginx server should now start with whatever value you supplied in the "environment" section of your docker-compose.yml

As mentioned in the solution above, you can also define your own entry point, however this solution has also proven to work pretty well, and keeps everything contained into a single configuration file, giving me the ability to run a stack of services directly from git with nothing but a docker-compose.yml file.

A big thank you to everybody who took the time to ready through this, and bellackn for taking the time to help me solve the issue.

这篇关于从docker-compose替换NGINX配置中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆