Php Nginx在docker上,curl获取连接被拒绝

Php Nginx在docker上,curl获取连接被拒绝,php,docker,nginx,curl,docker-compose,Php,Docker,Nginx,Curl,Docker Compose,我和docker在当地的环境中工作。 我有一个nginxweb容器和一个php容器,它们在同一个网络中 我从自己的dockerfile(带有phpfpm和phpcli)构建php容器;而且,nginx I从nginx:stablehub图像以docker compose的形式将其合成 我有两个项目:一个symfony(http://i-r4y.kaiza.lh/)和drupal(http://i-z4r4.kaiza.lh/)在其中运行。symfony公开了一个必须由drupal使用的api。问

我和docker在当地的环境中工作。 我有一个nginxweb容器和一个php容器,它们在同一个网络中

我从自己的dockerfile(带有phpfpm和phpcli)构建php容器;而且,nginx I从
nginx:stable
hub图像以docker compose的形式将其合成

我有两个项目:一个symfony(
http://i-r4y.kaiza.lh/
)和drupal(
http://i-z4r4.kaiza.lh/
)在其中运行。symfony公开了一个必须由drupal使用的api。问题是,当我从drupal调用symfony时出现错误
cURL错误7:无法连接到I-r4y.kaiza.lh端口80:连接被拒绝

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

但是,当我在php容器中输入bash时,我会卷曲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;
    }

  ...

    upstream php {
        server kz-php74:8080;
    }
}

对于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;
    }
...
    upstream php {
        server kz-php74:8080;
    }
...
}
有人知道为什么这不起作用吗


谢谢

您需要向docker主机公开端口80

看起来您正试图从docker主机(运行docker的真实机器)卷曲到在docker容器中运行的nginx

...
  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
...
您可以在docker compose中使用以下命令执行此操作:

web:
  image: nginx:stable
  …
  ports: 
    - 80:80
upstream php {
    # use the docker service name. I removed the . b/c I don't know if that works in a docker service name
    # This assumes the docker service has been renamed from php7.4 to php74 in docker-compose.yml
    server php74:8080; # or whatever port to which php is listening
}
这会让你进入nginx。然而,下一个障碍可能是ngxinx访问php服务

就像@AmyDev提到的,使用docker的名字解析会更好

在nginx配置中,需要以下行将nginx指向docker的内部DNS:

resolver 127.0.0.11 ipv6=off;
然后,您可以使用以下内容声明上游的

web:
  image: nginx:stable
  …
  ports: 
    - 80:80
upstream php {
    # use the docker service name. I removed the . b/c I don't know if that works in a docker service name
    # This assumes the docker service has been renamed from php7.4 to php74 in docker-compose.yml
    server php74:8080; # or whatever port to which php is listening
}

当您运行
curl时http://i-r4y.kaiza.lh
您向同一个容器(php7.4)发出请求,而不是在
web
(nginx)上。如果要向另一个容器发出请求,可以将容器服务名称用作域


尝试运行
curlhttp://web
在php容器中

我通过在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
...
当然,我需要在config nginx中添加url别名:

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

我不确定这里的
docker inspect
屏幕截图是否有用。如果您有一个docker-compose.yml文件,您应该将其内容粘贴到这里,cURL命令也是如此。你说他们在同一个网络中,你试过从另一个网络ping一个吗?我把截图放在docker compose和curl错误上。我留下docker inspect的截图,以显示它们在同一网络“kz_local”上。好的,不,我经常更新问题,但这有点太多:删除所有截图,并在问题中粘贴一些文本。要编写代码块,请在第一行使用
```
,然后使用代码,然后再次使用
``
关闭它。尝试使用容器的服务名称作为目标,这样更简单、更可靠:
curl-web
ha,当我使用“curl-kz-web”时,我有了一些东西:symfony-html。但问题是我必须从应用程序端调用它(drupal);所以我需要url调用才能工作(不是容器名称),谢谢你的回答。我试过你说的,但还是不行。我把我的nginx配置放在我的帖子里。如果你能更准确地帮助我,请告诉我事实上,在web容器中,url卷曲可以很好地工作。但问题在于php容器。谢谢您的回答。事实上,容器名称之间的直接调用是可能的,并且可以完美地工作。问题是从php容器调用url别名nginx。