Docker容器的Nginx反向代理

时间:2017-10-09 08:02:57

标签: docker nginx proxy docker-compose reverse-proxy

我的Docker服务的反向代理有问题。我有一台IP 10.0.0.163的本地机器,并且运行着Docker堆栈,使用nginx和portainer(对于这个问题,只有它们很重要)。

搬运工-compose.yml:

...
 portainer:
    image: portainer/portainer
    ports:
      - "9000:9000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      - "/mnt/StorageDrive/Portainer:/data"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      ...
      - proxy

 reverse-proxy:
    image: reverseproxy:latest
    ports:
      - "80:80"
    networks:
      - proxy

networks:
  ...
  proxy:

nginx.conf:

worker_processes  1;  ## Default: 1

events { worker_connections  1024; }

http {

  sendfile     on;

  server {
    listen 80;
    allow all;

    location / {
      proxy_pass      http://10.0.0.163:9000;
    }
  }
}

用于reverseeproxy图像的Dockerfile:

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

当尝试访问10.0.0.163时,我收到错误502并且来自reverseproxy的日志显示:

2017/10/09 07:43:02 [error] 5#5: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 10.255.0.2, server: , request: "GET / HTTP/1.1", upstream: "http://10.0.0.163:9000/", host: "10.0.0.163"
10.255.0.2 - - [09/Oct/2017:07:43:02 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"

在浏览器中输入10.0.0.163:9000/时,一切正常。问题是什么?如何使用此URL

10.0.0.163/portainer/... -> 10.0.0.163:9000/...

2 个答案:

答案 0 :(得分:2)

尝试更改nginx配置

  server {
    listen 80;
    allow all;

    location / {
      proxy_pass      http://portainer:9000/;
      resolver        127.0.0.11;
    }
  }

portainer是在docker-compose.yml文件中定义的容器名称 127.0.0.11是嵌入式docker DNS服务器

另外。替代方式。您可以使用jwilder/nginx-proxy代替反向代理。

答案 1 :(得分:1)

问题出在我身上。

我在下面解决

# Docker run command
docker run --name portainer --network devops-net -d -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

# Nginx
location /portainer/ {
  proxy_http_version 1.1;
  proxy_set_header Connection "";
  proxy_pass http://portainer:9000/;
}

location /portainer/api/websocket/ {
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
  proxy_http_version 1.1;
  proxy_pass http://portainer:9000/api/websocket/;
}

# Notice
Portainer default port 9000

打开浏览器>> https://domain-name.dev/portainer

enter image description here

了解更多:https://portainer.readthedocs.io/en/stable/faq.html

为我工作。 ^ ____ ^