docker 上的 PHP 和 Nginx,curl 在 php 容器中获取连接被拒绝

docker 上的 PHP 和 Nginx,curl 在 php 容器中获取连接被拒绝

我在本地环境中使用 docker 工作。我有一个 nginx web 容器和一个 php 容器,它们位于同一网络中。

我从自己的 dockerfile(使用 phpfpm 和 phpcli)构建 php 容器;并且,我从 hub 镜像中在 docker-compose 中编写 nginx nginx:stable

我有 2 个项目:一个 symfony( http://i-r4y.kaiza.lh/) 和一个http://i-z4r4.kaiza.lh/在其中运行的 drupal()。symfony 公开了一个必须由 drupal 使用的 api。问题是,当我从 drupal 调用 symfony 时出现错误cURL error 7: Failed to connect to i-r4y.kaiza.lh port 80: Connection refused

我认为它是 symfony 侧 api 路由的配置;就像它必须是公开的或者接受 CORS 等等...

在 php 容器中,当我卷曲 symfony 或 drupal url 时,我遇到了同样的错误。

app@kz-php74:/var/www$ curl http://i-r4y.kaiza.lh
curl: (7) Failed to connect to i-r4y.kaiza.lh port 80: Connection refused
app@kz-php74:/var/www$ curl http://i-z4r4.kaiza.lh
curl: (7) Failed to connect to i-z4r4.kaiza.lh port 80: Connection refused

我检查了主机所在的 php 容器/etc/hosts

app@kz-php74:/var/www$ cat /etc/hosts | grep i-
127.0.0.1   i-r4y.kaiza.lh
127.0.0.1   i-z4r4.kaiza.lh

这是docker-compose.yml:

version: '2.4'

services:
  php7.4:
    build:
      context: ../../../dockerfile
      dockerfile: Dockerfile.php
      args:
        PHP_VERSION: 7.4
    container_name: "kz-php74"
    hostname: "kz-php74"
    user: 1000:1000
    working_dir: /var/www
    volumes:
      - "${LOCAL_PATH}/../www:/var/www"
    extra_hosts:
      - "i-r4y.kaiza.lh:127.0.0.1"
      - "i-z4r4.kaiza.lh:127.0.0.1"
    networks:
      - kz_local

  mysql:
    container_name: kz-mysql
    image: mariadb:10.4.0
    volumes:
      - ${LOCAL_PATH}/.data/mariadb:/var/lib/mysql
      - ${LOCAL_PATH}/config/mariadb/conf.d/custom.cnf:/etc/mysql/conf.d/custom.cnf
      - ${LOCAL_PATH}/../www:/var/www
    ports:
      - ${MYSQL_PORT:-3306}:3306
    environment:
      MYSQL_ROOT_PASSWORD: password
    networks:
      - kz_local

  web:
    image: nginx:stable
    container_name: kz-web
    volumes:
      - ${LOCAL_PATH}/config/nginx/conf.d:/etc/nginx/conf.d
      - ${LOCAL_PATH}/../www:/var/www
    ports:
      - 80:80
    networks:
      - kz_local

networks:
  kz_local:
    external: true

drupal的nginx配置:

server {
    listen 80;
    listen [::]:80;
    server_name i-z4r4.kaiza.lh;

    root /var/www/i-z4r4/web;

    resolver 127.0.0.11 ipv6=off;
    
    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    # In Drupal 8, we must also match new paths where the '.php' appears in
    # the middle, such as update.php/selection. The rule we use is strict,
    # and only allows this pattern with the update.php front controller.
    # This allows legacy path aliases in the form of
    # blog/index.php/legacy-path to continue to route to Drupal nodes. If
    # you do not have any paths like that, then you might prefer to use a
    # laxer rule, such as:
    #   location ~ \.php(/|$) {
    # The laxer rule will continue to work if Drupal uses this new URL
    # pattern with front controllers other than update.php in a future
    # release.
    location ~ '\.php$|^/update.php' {
        set $fastcgi_pass "kz-php74:9000";

        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        # Security note: If you're running a version of PHP older than the
        # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
        # See http://serverfault.com/q/627903/94922 for details.
        include fastcgi_params;
        # Block httpoxy attacks. See https://httpoxy.org/.
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_pass $fastcgi_pass;
    }

  ...

}

对于 symfony:

server {
    listen 80;
    listen [::]:80;
    server_name i-r4y.kaiza.lh;

    root /var/www/i-r4y/public;

    resolver 127.0.0.11 ipv6=off;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        set $fastcgi_pass "kz-php74:9000";

        fastcgi_pass $fastcgi_pass;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }
...
}

有人知道为什么这不起作用吗?

谢谢

答案1

我通过在 Web 容器的网络中添加一个别名解决了该问题,通过该别名我可以从 php 容器访问。

...
  web:
    image: nginx:stable
    ....
    networks:
      kz_local:
        # To allow fetch (call, curl) from php container.
        aliases:
          - api.i-r4y.kaiza.lh
          - api.i-z4r4.kaiza.lh
...

当然,我需要在配置 nginx 中添加 url 别名:

...
server_name i-z4r4.kaiza.lh api.i-z4r4.kaiza.lh;
...

相关内容