【发布时间】:2019-11-01 03:01:16
【问题描述】:
我正在尝试在通过 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 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;
}
下面是 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;"]
以下是 NGINX 服务器的 docker-compose.yml 部分(名称和 IP 已更改)。 envsubst 命令在我的故障排除中被有意注释掉了。
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;'"
似乎正在发生的事情是,当我在上游块中引用 $nginxproxy 变量时(就在“服务器”之后),我得到的输出使它看起来像是在引用字符串文字“$nginxproxy”而不是替换值变量。
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
当我尝试使用 envsubst 时,我收到一个错误,听起来像是命令与 nginx.conf 文件的格式混淆了
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
我很困惑,所以提前感谢您的帮助。
【问题讨论】:
标签: docker nginx docker-compose environment-variables