繁体   English   中英

连接被拒绝

[英]Nginx on docker, curl get Connection refused

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

我从我自己的 dockerfile(使用 phpfpm 和 phpcli)构建 php 容器; 并且,我将 nginx 组合在nginx:stable hub image 的 docker-compose 中。

我有 2 个项目:一个在其中运行的 symfony( http://i-r4y.kaiza.lh/ )和一个 drupal( http://i-z4r4.kaiza.lh/ )。 并且 symfony 暴露了 api 必须由 drupal 使用。 问题是当我从 drupal cURL error 7: Failed to connect to i-r4y.kaiza.lh port 80: Connection refused

我以为是symfony侧api路由的配置; 喜欢它必须是公开的或接受 CORS 等...

but when I enter on bash in the php container , and I do curl either the symfony or drupal url, I have the same error.

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;
    }
...
}

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

谢谢

您需要将端口 80 暴露给您的 docker 主机。

It looks like you are trying to curl from your docker host (your real machine running docker) to nginx running in a docker container.

您可以在 docker-compose 中执行以下操作:

web:
  image: nginx:stable
  …
  ports: 
    - 80:80

这将使您到达 nginx。 但是,您的下一个障碍可能是 ngxinx 到达您的 php 服务。

就像@AmyDev 提到的那样,使用 docker 的名称解析会更好地为您服务。

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

resolver 127.0.0.11 ipv6=off;

然后您可以使用以下内容声明您的upstream

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)上。 如果要向另一个容器发出请求,可以使用容器服务名称作为域。

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

我通过在 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;
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM