xdebug 连接拒绝 Docker 容器

xdebug connection refused to Docker container

错误:

2020/04/26 23:43:48 [error] 8#8: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.208.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://192.168.208.3:9000", host: "127.0.0.1", referrer: "http://127.0.0.1/"

无法与 xdebug 建立连接。 Docker 配置取自此处 https://gitlab.com/martinpham/symfony-5-docker/-/tree/master/docker

xdebug 是单独安装的,它被 IDE 识别。

还在php-fpm环境下的docker-compose.yml中添加了这个:

environment:
- XDEBUG_CONFIG:remote_host=host.docker.internal remote_enable=1 remote_autostart=off xdebug.idekey=PHPSTORM

还需要什么added/modified?

这就是我最近为 http 服务设置 docker + php + xdebug 的方式。我指导我的同龄人完成了它并且完美无缺。

1。将 ENV PHP_IDE_CONFIG 添加到您的 docker fpm 池配置

您需要将此环境添加到您的 php-fpm 池配置中。可以是 www.conf(例如)

env[PHP_IDE_CONFIG] = "serverName=localhost"

2。将 xdebug.ini 添加到您的 docker 容器

这是我用于设置的 xdebug.ini 的示例:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = off
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9000
xdebug.idekey = PHPSTORM
xdebug.max_nesting_level = 1500

3。 [IntelliJ IDEA 或 PHPStorm] - 设置 PHP 服务器

  1. 打开Preferences
  2. 前往 Languages & Frameworks -> PHP -> Servers
  3. Name 设置为 localhost (这很重要,应该匹配 PHP_IDE_CONFIG 值)
  4. Host设置为localhost
  5. 启用use path mappings
  6. 将您的项目根路径映射到 docker workdir(例如 /var/www/html)以便 IntelliJ 可以正确映射路径。

4。 [IntelliJ IDEA 或 PHPStorm] - 设置 IDE 键

  1. 打开Preferences
  2. 前往 Languages & Frameworks -> PHP -> Debug -> DGBp proxy
  3. IDE 键 设置为 PHPSTORM

5。将 XDEBUG_SESSION=PHPSTORM 添加到您的 url/cookie..

最后:

  • ?XDEBUG_SESSION=PHPSTORM 添加到您的 url 或
  • 添加名称为 XDBEUG_SESSION 且值为 PHPSTORM
  • 的 cookie

问题出现在: 环境:

- XDEBUG_CONFIG:remote_host=host.docker.internal remote_enable=1 remote_autostart=off xdebug.idekey=PHPSTORM
  1. 未正确解析。
  2. 如果我只传递 remote_host=host.docker.internal 那么它将传递 "localhost" 而不是主机 IP 地址。

我知道 post 是为 PHPStorm 用户准备的,但是如果任何 VSCode 用户偶然发现这里,那么有两件事需要做不同于 PHPStorm 的事情(更多关于 PHPStorm ) -

  1. 传递主机的 IP 地址 (192.168...) 而不是 host.docker.internal
  2. 在调试启动配置中配置 pathMappings -
{
    "name": "Debug Docker",
    "type": "php",
    "request": "launch",
    "port": 9000,
    "pathMappings": {
        "/var/www/app": "${workspaceFolder}"
    }
},

/var/www/app换成你自己的路径!