Nginx作为docker容器的反向代理

时间:2016-11-08 08:23:10

标签: nginx docker reverse-proxy

我正在尝试让Nginx将局域网内的代理连接转换为多个Web应用程序,包括docker容器内的应用程序。

使用proxy_pass网址

可以访问这两个网络应用程序

我正在使用以下dockerfile:

# Set the base image to Ubuntu
FROM ubuntu

RUN apt-get update
RUN apt-get install -y nginx

RUN rm -v /etc/nginx/nginx.conf
RUN echo "daemon off; \n\
 \n\
worker_processes 1; \n\
events { worker_connections 1024; } \n\
 \n\
http { \n\
 \n\
    server { \n\
        listen 99; \n\
 \n\
        server_name dashboard; \n\
        location / { \n\
            proxy_pass http://dashboard:80; \n\
        } \n\
        location /app1 { \n\
            proxy_pass http://otherhostname:9000/app1; \n\
        } \n\
    } \n\
} \n\

" >> /etc/nginx/nginx.conf
EXPOSE 99
CMD service nginx start

当将其作为服务(容器)运行时,我可以访问app1,但不能访问仪表板。

奇怪的是我之前有这个工作,我很确定我没有改变dockerfile的任何基础。我错过了什么吗?

编辑:(我目前在端口80上公开了仪表板,并在99上使用nginx进行测试)

我用:

运行nginx容器
docker service create \
    --replicas 1 \
    --name nginx \
    -p 99:99 \
    nginx_image

仪表板也暴露了正确的端口。

docker service create \
    --replicas 1 \
    --name dashboard \
    -p 80:8080 \
    dashboard_image

查看nginx error.log,我发现:

2016/11/08 08:46:41 [error] 25#25: *42 upstream timed out (110: Connection timed out) while connecting to upstream, client: 10.255.0.3, server: dashboard, request: "GET / HTTP/1.1", upstream: "http://dockerhostip:80/", host: "dashboard:99"

1 个答案:

答案 0 :(得分:0)

Nginx按预期工作。我发现在将代理传递更改为example.com时,它可以正常工作。它必须是在仪表板中发生变化的东西,这会使事情变得混乱。

相关问题