Jwilder nginx 代理 - docker 组合结构更新后 503

Jwilder nginx proxy - 503 after docker compose structure update

我正在使用 jwilder/nginx-proxy 和单独的 docker-compose.yaml。看起来像这样:

proxy:
   image: jwilder/nginx-proxy
   restart: always
   volumes:
     - /var/run/docker.sock:/tmp/docker.sock:ro
     - ./nginx/conf.d/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
     - /Users/marcin/Docker/local_share/certificates:/etc/nginx/certs:ro      
   ports:
     - "80:80"
     - "443:443"
   container_name: proxy

我已经使用它很长时间了,当我的项目 docker-compose.yaml 看起来像这样时它工作正常:

web:
  build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
  volumes:
    - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
    - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
    - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
    - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
    - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
    - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
    - /Users/marcin/Docker/local_share/:/root/.local_share/
    - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
  working_dir: /usr/share/nginx/html/
  links:
    - db
  container_name: test.php
  hostname: test.local
  ports:
    - "336:22"
    - "8081:80"
    - "18080:443"    
  environment:
    - VIRTUAL_HOST=test.local   
    - CERT_NAME=default
    - HTTPS_METHOD=noredirect
db:
  build: /Users/marcin/Docker/definitions/mysql/5.7
  environment:
     - MYSQL_ROOT_PASSWORD=pass
     - MYSQL_DATABASE=
     - MYSQL_USER=
     - MYSQL_PASSWORD=
  expose:
     - 3306
  volumes:
    - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
    - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
    - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
  ports:
    - "33060:3306"
  container_name: test.db
  hostname: test.local

我可以使用预期的 http://test.localhttps://test.local 毫无问题地访问网站。

但是我必须将我的文件结构更新到更新的版本:

version: "3.2"
services:
  web:
    build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
    volumes:
      - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
      - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
      - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
      - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
      - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
      - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
      - /Users/marcin/Docker/local_share/:/root/.local_share/
      - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
    working_dir: /usr/share/nginx/html/
    links:
      - db
    container_name: test.php
    hostname: test.local
    ports:
      - "336:22"
      - "8081:80"
      - "18080:443"    
    environment:
      - VIRTUAL_HOST=test.local   
      - CERT_NAME=default
      - HTTPS_METHOD=noredirect
  db:
    build: /Users/marcin/Docker/definitions/mysql/5.7
    environment:
       - MYSQL_ROOT_PASSWORD=pass
       - MYSQL_DATABASE=
       - MYSQL_USER=
       - MYSQL_PASSWORD=
    expose:
       - 3306
    volumes:
      - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
      - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
      - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
    ports:
      - "33060:3306"
    container_name: test.db
    hostname: test.local

之后好像就不行了。我可以毫无问题地使用 ip 和端口访问站点,但我不能再使用域来访问它。当我尝试时,我得到:

503 Service Temporarily Unavailable

nginx/1.13.8

这肯定来自 jwilder nginx(而不是项目中的 nginx)。

所以问题是 - 我应该把环境变量放在哪里才能让它工作?似乎当它们按原样放置时,它们不会被代理读取。

503 表示 nginx-proxy 容器可以在 docker 中看到您的容器 运行ning 并且它具有 nginx 将流量路由到它所需的配置,但无法连接通过 docker 网络到该容器。 要使容器到容器的网络正常工作,您需要定义一个通用的 docker 网络。 您应该首先 运行 创建一个网络:

docker network create proxy

然后更新你的 nginx-proxy compose 文件以使用网络(这也应该至少升级到 v2 语法,我已经使用 3.2 来匹配你的其他文件):

version: "3.2"

networks:
  proxy:
    external: true

services:
  proxy:
    image: jwilder/nginx-proxy
    restart: always
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - ./nginx/conf.d/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
      - /Users/marcin/Docker/local_share/certificates:/etc/nginx/certs:ro      
    ports:
      - "80:80"
      - "443:443"
    container_name: proxy
    networks:
      - proxy

然后为您的应用做类似的事情:

version: "3.2"
networks:
  proxy:
    external: true
services:
  web:
    build: /Users/marcin/Docker/definitions/php-nginx/php-7.1-ubuntu
    volumes:
      - /Users/marcin/Docker/projects/test.local/html/:/usr/share/nginx/html/
      - /Users/marcin/Docker/projects/test.local/nginx/conf.d/:/etc/nginx/conf.d/
      - /Users/marcin/Docker/projects/test.local/nginx/log/:/var/log/nginx/
      - /Users/marcin/Docker/projects/test.local/supervisor/conf.d/:/etc/supervisor/conf.d/
      - /Users/marcin/Docker/projects/test.local/supervisor/log/:/var/log/supervisor/
      - /Users/marcin/Docker/projects/test.local/cron/:/root/.cron/
      - /Users/marcin/Docker/local_share/:/root/.local_share/
      - /Users/marcin/Docker/local_share/certificates/:/usr/share/nginx/certificates/  
    working_dir: /usr/share/nginx/html/
    links:
      - db
    container_name: test.php
    hostname: test.local
    ports:
      - "336:22"
      - "8081:80"
      - "18080:443"    
    environment:
      - VIRTUAL_HOST=test.local   
      - CERT_NAME=default
      - HTTPS_METHOD=noredirect
    networks:
      - proxy
      - default
  db:
    build: /Users/marcin/Docker/definitions/mysql/5.7
    environment:
       - MYSQL_ROOT_PASSWORD=pass
       - MYSQL_DATABASE=
       - MYSQL_USER=
       - MYSQL_PASSWORD=
    expose:
       - 3306
    volumes:
      - /Users/marcin/Docker/projects/test.local/mysql/data/:/var/lib/mysql/
      - /Users/marcin/Docker/projects/test.local/mysql/conf.d/:/etc/mysql/conf.d/source
      - /Users/marcin/Docker/projects/test.local/mysql/log/:/var/log/mysql/
    ports:
      - "33060:3306"
    container_name: test.db
    hostname: test.local

如果您从 v1 语法升级(未定义版本),您会发现 docker 从 运行 在同一网络上不使用 dns 的所有内容切换到 运行使用 dns 将每个 compose 项目或堆栈放在专用网络上。要 运行 您在其他网络上的应用,您需要对其进行明确配置。在上面的例子中,只有 web 容器被放置在代理网络上,两者都在为此项目或堆栈创建的默认网络上。