我正在使用Docker构建一个多容器应用程序。 full environment is on github如果您希望重新创建它,但为方便起见,我将我认为的相关部分包括在内。我的nginx Dockerfile是这样的:
FROM ubuntu:14.04
MAINTAINER Garry Cairns
ENV REFRESHED_AT 2015-02-11
# get the nginx package and set it up
RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "nginx"]
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
VOLUME ["/var/cache/nginx"]
EXPOSE 80 443
# load nginx conf
ADD ./site.conf /etc/nginx/sites-available/correspondence
RUN ["ln", "-s", "/etc/nginx/sites-available/correspondence", "/etc/nginx/sites-enabled/correspondence"]
CMD ["nginx", "-g", "daemon off;"]
添加到启用站点的site.conf文件如下所示:
# see http://serverfault.com/questions/577370/how-can-i-use-environment-variables-in-nginx-conf#comment730384_577370
upstream api {
server api_1:8000;
}
server {
location / {
proxy_pass http://api;
}
}
nginx配置似乎与this answer中找到的类似,我觉得这种方法应该没问题。但是当我在我的机器上访问本地主机(没有端口)或生产我的域时,我只获得了nginx欢迎页面,没有转发。
我可以连接到正在运行的nginx容器并使用http://api_1:8000
从应用容器中获取正确的结果,所以我非常确定问题出在我的nginx设置而不是我的Docker,但我可以&# 39; t拼出这一个。有没有其他人在过去解决过这个问题?
答案 0 :(得分:2)
终于弄明白了。我需要删除使用nginx安装的默认启用站点。我的Dockerfile现在显示为:
FROM ubuntu:14.04
MAINTAINER Garry Cairns
ENV REFRESHED_AT 2015-02-11
# get the nginx package and set it up
RUN ["apt-get", "update"]
RUN ["apt-get", "-y", "install", "nginx"]
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
VOLUME ["/var/cache/nginx"]
EXPOSE 80 443
# load nginx conf
ADD ./site.conf /etc/nginx/sites-available/correspondence
RUN ["ln", "-s", "/etc/nginx/sites-available/correspondence", "/etc/nginx/sites-enabled/correspondence"]
RUN ["rm", "-rf", "/etc/nginx/sites-available/default"]
CMD ["nginx", "-g", "daemon off;"]
世界上一切都很好。