我正在尝试遵循this指南,使用另一个具有nginx实例的容器作为反向代理,为docker容器设置反向代理(提供静态文件)。
我希望看到我的页面在/
上投放,但是由于错误消息,我被阻止在构建中:
container_nginx_1 | 2020/05/10 16:54:12 [emerg] 1#1: host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
container_nginx_1 | nginx: [emerg] host not found in upstream "container1:8001" in /etc/nginx/conf.d/sites-enabled/virtual.conf:2
nginx_docker_test_container_nginx_1 exited with code 1
基于下面给出的示例和其他各个页面,我已经尝试了以下virtual.conf
文件的多种变体,这是当前的变体:
upstream cont {
server container1:8001;
}
server {
listen 80;
location / {
proxy_pass http://cont/;
}
}
如果您愿意查看第三方网站,我已经做了一个最小的仓库here,否则,下面是最相关的文件。
我的docker-compose文件如下所示:
version: '3'
services:
container1:
hostname: container1
restart: always
image: danjellz/http-server
ports:
- "8001:8001"
volumes:
- ./proj1:/public
command: "http-server . -p 8001"
depends_on:
- container_nginx
networks:
- app-network
container_nginx:
build:
context: .
dockerfile: docker/Dockerfile_nginx
ports:
- 8080:8080
networks:
- app-network
networks:
app-network:
driver: bridge
和Dockerfile
# docker/Dockerfile_nginx
FROM nginx:latest
# add nginx config files to sites-available & sites-enabled
RUN mkdir /etc/nginx/conf.d/sites-available
RUN mkdir /etc/nginx/conf.d/sites-enabled
ADD projnginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-available/virtual.conf
RUN cp /etc/nginx/conf.d/sites-available/virtual.conf /etc/nginx/conf.d/sites-enabled/virtual.conf
# Replace the standard nginx conf
RUN sed -i 's|include /etc/nginx/conf.d/\*.conf;|include /etc/nginx/conf.d/sites-enabled/*.conf;|' /etc/nginx/nginx.conf
WORKDIR /
我正在使用docker-compose up
运行它。
答案 0 :(得分:1)
问题是,如果无法在upstream
块中解析主机名,nginx将不会启动。在这里,您已将服务container1
定义为依赖于container_nginx
。但是由于容器1主机名未解析(因为容器1尚未启动)的事实,nginx容器永远不会启动。您不认为应该反向吗? Nginx容器应依赖于应用程序容器。
另外,在您的nginx端口绑定中,您已映射8080:8080
,而在nginx conf中,您有80个监听。