【问题标题】:Docker - Access nginx container using nginx server_name from another containerDocker - 使用来自另一个容器的 nginx server_name 访问 nginx 容器
【发布时间】:2018-06-29 14:01:37
【问题描述】:

我有一个 node.js 应用程序(web-app)和两个 lumen 应用程序(api、customer-api),它们由一个监听端口 80 的 nginx 容器进行负载平衡。

我的 docker-compose.yml 文件:

version: '2'
services:
  nginx:
    build:
      context: ../
      dockerfile: posbytz-docker/nginx/dockerfile
    volumes:
      - api
      - customer-api
    ports:
      - "80:80"
    networks:
      - network
    depends_on:
      - web-app
      - api
      - customer-api
  web-app:
    build:
      context: ../
      dockerfile: posbytz-docker/web-app-dockerfile
    volumes:
      - ../web-app:/posbytz/web-app
      - /posbytz/web-app/node_modules
    ports:
      - "3004:3004"
    networks:
      - network
  api:
    build:
      context: ../
      dockerfile: posbytz-docker/api-dockerfile
    volumes:
      - ../api:/var/www/api
    networks:
      - network
  customer-api:
    build:
      context: ../
      dockerfile: posbytz-docker/customer-api-dockerfile
    volumes:
      - ../customer-api:/var/www/customer-api
    networks:
      - network
  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - network
  memcached:
    image: memcached
    ports:
      - "11211:11211"
    networks:
      - network
  mysql:
    image: mysql:5.7
    volumes:
      - ./db-data:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - network
  adminer:
    image: adminer
    restart: always
    ports:
      - "9001:8080"
    networks:
      - network
networks:
  network:
    driver: bridge

由于我使用的是桥接网络,因此我可以使用容器名称从另一个容器访问每个容器。但我想要的是,使用它们的 nginx 配置的 server_name 访问容器。

下面是各个应用的nginx配置,

web-app.conf:

server {
  listen 80;
  server_name posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    proxy_pass http://web-app:3004;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

api.conf:

server {
  listen 80;
  index index.php index.html;
  root /var/www/api/public;
  server_name api.posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    try_files $uri /index.php?$args;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass api:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

customer-api.conf

server {
  listen 80;
  index index.php index.html;
  root /var/www/customer-api/public;
  server_name customer-api.posbytz.local;
    resolver 127.0.0.11 valid=10s;

  location / {
    try_files $uri /index.php?$args;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass customer-api:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
  }
}

问题

我想从 web-app 容器访问 api 和 customer-api 容器。问题是当我尝试curl http://nginx 时,我只得到来自 api 容器的响应。有没有办法通过nginx容器访问customer-api容器?

我尝试了什么

当我在 web-app 容器的 /etc/hosts 文件中手动映射 nginx 容器 (172.21.0.9) 的 IP 和它们各自的 server_name 时,它​​似乎可以工作。

我在 web-app 容器的 /etc/hosts 文件中添加的内容:

172.21.0.9  api.posbytz.local
172.21.0.9  customer-api.posbytz.local

有没有其他方法可以在没有人工干预的情况下实现这一点?

【问题讨论】:

  • 你不能在 web-app 容器中使用 http://apihttp://customer-api 吗?(也许你应该在 url 中使用端口,即 http://api:9000)。我认为通过服务名称作为主机从某个容器访问容器是一种常见的做法。不是通过另一个容器反向代理。另外,我想知道,为什么 nginx 容器将 http://nginx 解析为 api 应用程序...
  • @SlavaNikulin,我想通过反向代理访问这两个 api,因为它们配置了 nginx fastcgi 转发到 php-fpm,所以http://api:9000 在这种情况下不起作用,http://nginx 解析为 api 应用程序因为它监听 80 端口并转发到http://api:9000

标签: docker nginx docker-compose lumen


【解决方案1】:

最后通过更改 customer-api.conf 上的 nginx 配置以侦听端口 81 即使其工作。 listen 80;listen 81;。现在http://nginx 解析为http://api:9000http://nginx:81 解析为http://customer-api:9000

【讨论】:

  • 基本上这不是一个答案,而是一个尖峰。它不可扩展。也许有人知道如何在 docker 中处理这个问题的正确答案?
【解决方案2】:

你可以使用别名:

networks:
  some-network:
    aliases:
      - api.posbytz.local
      - customer-api.posbytz.local

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2022-01-11
    • 1970-01-01
    • 2018-09-27
    • 2019-06-13
    • 2021-12-23
    相关资源
    最近更新 更多