【问题标题】:Deployed Docker-Compose returns "502 Bad Gateway"部署的 Docker-Compose 返回“502 Bad Gateway”
【发布时间】:2019-05-29 21:39:32
【问题描述】:

请帮忙。

我找到了一篇博文,https://blog.ssdnodes.com/blog/host-multiple-ssl-websites-docker-nginx/),关于部署多个具有相同 nginx-proxy 但具有不同 VIRTUAL_HOST 名称的 docker-compose 应用程序

但由于某种原因,两个应用程序都返回错误 502 Bad Gateway

以下错误是我在运行docker-compose logs nginx 时看到的

2019/05/29 20:52:26 [error] 8#8: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 52.209.30.187, server: gregsithole.com, request: "GET / HTTP/1.1", upstream: "http://172.20.0.5:80/", host: "gregsithole.com"

而且我相信upstream 使用的是内部 docker 网络 IP,因为这不是我的服务器的 IP。我的上游由以下文件确定:https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl

但我不太熟悉它的工作原理。

以下是我的 docker-compose 文件的示例:

nginx-proxy/docker-compose.yaml

version: "3.6"
services:
  nginx:
    image: nginx
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
    labels:
      - "com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy=true"

  dockergen:
    image: jwilder/docker-gen
    container_name: nginx-proxy-gen
    restart: always
    depends_on:
      - nginx
    command: -notify-sighup nginx-proxy -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro

  letsencrypt:
    image: jrcs/letsencrypt-nginx-proxy-companion
    container_name: nginx-proxy-le
    restart: always
    depends_on:
      - nginx
      - dockergen
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
      NGINX_DOCKER_GEN_CONTAINER: nginx-proxy-gen
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro

volumes:
  conf:
  vhost:
  html:
  certs:

networks:
  default:
    external:
      name: nginx-proxy

dockerized-ghost/docker-compose.yaml

version: "3.6"
services:

  ghost:
    image: ghost
    restart: always
    expose:
      - 80
    volumes:
      - ../../ghost:/var/lib/ghost/content
    environment:
      NODE_ENV: production
      url: https://blog.gregsithole.com
      VIRTUAL_HOST: blog.gregsithole.com
      LETSENCRYPT_HOST: blog.gregsithole.com
      LETSENCRYPT_EMAIL: hidden-email

networks:
  default:
    external:
      name: nginx-proxy

请帮忙

【问题讨论】:

  • 您可以发布您的解决方案吗?我需要知道为什么我的回答不适用于这个问题。
  • @filipe,您的解决方案是我需要将网络(我创建的)分配给每个服务,但是正如我们所说的那样,这并没有解决它......我最初以nginxdocker-genletsencrypt-nginx-proxy-companion 开始.然后我在验证它是否有效后添加了letsencrypt-nginx-proxy-companion。我已经添加了解决方案,请在下面查看我的答案

标签: docker nginx docker-compose bad-gateway


【解决方案1】:

您应该将网络 nginx-proxy 分配给服务 ghost:

  ghost:
    networks:
      - nginx-proxy
    ...

同样将newtwork分配给nginx

  nginx:
    networks:
      - nginx-proxy
    ...

同样把网络配置这样:

networks:
  nginx-proxy:
    external: true
  default:

这就是你所需要的。请记住,在 docker compose 文件中,您必须将网络声明为外部网络,但这还不够。您还可以将其单独分配给您希望成为网络一部分的每个服务。

我建议你升级到 Træfik 或 envoy。除非您付费,否则 Nginx 的可扩展性是有限的。

【讨论】:

  • 感谢您的回复,我尝试了这个并更改了 nginx 和 ghost,导致服务 nginx 使用未定义的网络“nginx-proxy”。然后我将其更改为networks: nginx-proxy: external: true,上游现在指向虚拟主机,但为什么我仍然会收到 502 Bad Gateway 错误?
  • 表示nginx连接不上upstream。检查 ghost 容器访问日志 docker ps 和 docker logs -f container_name 或 container_id。容器在运行吗?
  • 同时检查letsencrypt和dockergen日志。证书生成成功了吗?
  • 所以我检查了容器,据我所见,它们似乎都没有问题。还检查了让我们加密工作正常,因为我的两个应用程序都是安全的,即使它们返回 502 Bad Gateway...谢谢,我会查看链接并回复您
【解决方案2】:

在这个问题上花了几天时间尝试了各种解决方案。多次更新我的回购。我设法修复它。

我使用的博客文章已过时,因为它写于 2017 年,在同一个博客上我发现了最新文章 (https://blog.ssdnodes.com/blog/host-multiple-websites-docker-nginx/),在检查差异时我的 nginx-proxy 使用了 nginx , 'jwilder/docker-gen' 和 jrcs/letsencrypt-nginx-proxy-companion.

最新的文章只使用了jwilder/nginx-proxy,但我将其修改为还包括jrcs/letsencrypt-nginx-proxy-companion,请参阅下面的解决方案:

version: "3.6"
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - conf:/etc/nginx/conf.d
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - dhparam:/etc/nginx/dhparam
      - certs:/etc/nginx/certs:ro
      - /var/run/docker.sock:/tmp/docker.sock:ro

  letsencrypt-nginx-proxy-companion:
    image: jrcs/letsencrypt-nginx-proxy-companion
    environment:
      NGINX_PROXY_CONTAINER: nginx-proxy
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - vhost:/etc/nginx/vhost.d
      - html:/usr/share/nginx/html
      - certs:/etc/nginx/certs

volumes:
  conf:
  vhost:
  html:
  dhparam:
  certs:

networks:
  default:
    external:
      name: nginx-proxy

另外,我的另一个问题是,ghost 默认使用的端口是2368,所以我必须将其绑定到使用端口 80。所以我的解决方案不是在 ghost 上暴露端口 80,而是创建一个nginx 服务,该服务公开端口 80。

以下是我的幽灵设置:

version: "3.6"
services:

  ghost:
    image: ghost
    restart: always
    volumes:
      - ../../ghost:/var/lib/ghost/content
    environment:
      - VIRTUAL_HOST=blog.domain.com
      - LETSENCRYPT_HOST=blog.domain.com
      - LETSENCRYPT_EMAIL=name@domain.com
      - NODE_ENV=production
      - url=https://blog.domain.com

  nginx:
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
    expose:
      - 80
    depends_on:
      - ghost
    links:
      - ghost

networks:
  default:
    external:
      name: nginx-proxy

所以我能够让我的网站 (https://gregsithole.com) 和我的博客 (https://blog.gregsithole.com) 在同一个代理下工作

【讨论】:

    【解决方案3】:

    我能够使用 Greg 的答案来解决这个问题,只需要进行一个调整:我没有将 nginx 服务添加到 ghost docker-compose.yml 文件,而是添加了 VIRTUAL_PORT=2368,最后一切都为我准备好了.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2018-08-06
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2020-06-14
      • 2021-02-24
      相关资源
      最近更新 更多