简体   繁体   English

如何将 XDebug 与 nginx 反向代理后面的 PHP 上游一起使用?

[英]How can I use XDebug with a PHP upstream behind an nginx reverse proxy?

I have a PHP server running through PHP-FPM which is served with fastcgi through nginx on port 7000 .我有一个通过 PHP-FPM 运行的 PHP 服务器,它通过端口7000上的 nginx 与 fastcgi 一起提供。 This application has been dockerized and is running as a running container, eg my_api .此应用程序已被 dockerized 并作为正在运行的容器运行,例如my_api

The my_api docker container can be connected to directly via port 7000 (for sanity checking) as well as through another container which acts as an nginx reverse proxy that uses upstreams to expose the my_api application (and others) on ports 80 and 443 (port 80 redirects to SSL) through proxy_pass directives under the appropriate locations. my_api容器可以通过端口7000 (用于完整性检查)直接连接,也可以通过另一个容器作为 nginx 反向代理,使用上游在端口80443 (端口80 )上公开my_api应用程序(和其他应用程序)重定向到 SSL)通过适当位置下的proxy_pass指令。

If I start an XDebug session using dbgp on port 9000 directly against a file served from http://localhost:7000 I can see the debugging session established correctly and I can debug.如果我在端口9000上使用dbgp直接针对从http://localhost:7000提供的文件启动 XDebug 会话,我可以看到正确建立的调试会话,我可以进行调试。

However, if I attempt to start the XDebug session against the URL served by the nginx reverse proxy, eg https://localhost/my-api , the debug session does not appear to start or at least it doesn't create the connection properly (no breakpoints are hit in my IDE, etc.).但是,如果我尝试针对 nginx 反向代理提供的 URL 启动 XDebug 会话,例如https://localhost/my-api ,则调试会话似乎没有启动,或者至少它没有正确创建连接(在我的 IDE 等中没有命中断点)。

How can I establish an XDebug session for requests made through the nginx reverse proxy?如何为通过 nginx 反向代理发出的请求建立 XDebug 会话?

For purposes of this question, below is a (relevant) sample of my docker-compose.yml configuration and xdebug.ini :出于这个问题的目的,下面是我docker-compose.yml配置和xdebug.ini的(相关)示例:

docker-compose.yml : docker-compose.yml

version: "2"

services:
  api:
    build: <path_to_dockerfile>
    ports:
       - 7000:7000
       #- 9000:9000   # may be uncommented for direct debugging access
  nginx_proxy:
    build: <path_to_dockerfile>
    links:
      ...
      - api:api
    ports:
      - 80:80
      - 443:443

xdebug.ini调试文件

zend_extension=xdebug.so
xdebug.remote_enable=true
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_autostart=0

NB: I've tried a couple of different configurations to try and get this working, including launching a Docker container running a dbgpproxy but nothing seems to allow me to debug requests which pass through the reverse proxy.注意:我尝试了几种不同的配置来尝试使其正常工作,包括启动运行dbgpproxy的 Docker 容器,但似乎没有任何东西允许我调试通过反向代理的请求。 It is very possible though that the configuration I was using for these attempts was just wrong.尽管我用于这些尝试的配置很可能是错误的。

I have a few theories on what my problems may be, among them the suspicion that it is the reverse proxy's IP address that is being communicated to XDebug via the remote_connect_back configuration property.我有一些关于我的问题可能是什么的理论,其中包括怀疑是反向代理的 IP 地址通过remote_connect_back配置属性传递给 XDebug。

Any help or insight into how to properly configure XDebug to work with requests that are made to a server via nginx proxy passes to an upstream server would be greatly appreciated!任何有关如何正确配置 XDebug 以处理通过 nginx 代理传递到上游服务器向服务器发出的请求的任何帮助或见解将不胜感激!

I can provide further details if it would be helpful!如果有帮助,我可以提供更多详细信息!

Here's how I got PHP Storm to connect to a dockerized php-fpm / nginx application:以下是我如何让 PHP Storm 连接到 dockerized php-fpm / nginx 应用程序:

Inject the remote host IP into the container.将远程主机 IP 注入容器。 In your host, set the variable:在您的主机中,设置变量:

XDEBUG_HOST=$(ipconfig getifaddr en0)

I'm not too familiar with docker-compose.我对 docker-compose 不太熟悉。 I'm using a Kubernetes manifest, but I'm sure there's a way to inject environment variables.我正在使用 Kubernetes 清单,但我确信有一种方法可以注入环境变量。

in xdebug.ini:在 xdebug.ini 中:

xdebug.remote_host=${XDEBUG_HOST}

Now you should be able to set up your xdebug client to listen on xdebug.remote_port for debug connections.现在你应该能够设置你的 xdebug 客户端来监听xdebug.remote_port的调试连接。 You'll also have to set up a debug server in PHP Storm or whatever IDE you're using that points to http://127.0.0.1:8080 (or whatever port you're port-forwarding the nginx container to).您还必须在 PHP Storm 或您使用的任何指向http://127.0.0.1:8080 (或您将 nginx 容器端口转发到的任何端口)的 IDE 中设置调试服务器。

Here's what my setup looks like.这是我的设置的样子。 I'm using PHP Storm but I'm sure you can adapt this to other xdebug clients.我正在使用 PHP Storm,但我相信您可以将其应用于其他 xdebug 客户端。

PHP Storm Xdebug 服务器设置

PHP Storm 首选项

xdebug.ini: xdebug.ini:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=10000
xdebug.remote_autostart=1
xdebug.idekey=www-data
xdebug.remote_host=${XDEBUG_HOST}

Reference: https://shippingdocker.com/xdebug/参考: https : //shippingdocker.com/xdebug/

Being on windows I needed to disable also the option remote_connect_back (on linux it was not needed though)在 Windows 上,我还需要禁用选项remote_connect_back (虽然在 linux 上不需要)

xdebug.remote_connect_back = 0
xdebug.remote_host = host.docker.internal

Although it is quite late to answer but I have been trying very hard to implement this idea of xdebug ie xdebug a website behind an NGINX reverse proxy .虽然现在回答已经很晚了,但我一直在努力实现xdebug这个想法,即xdebug一个位于NGINX reverse proxy后面的网站。 However, I've concluded that there's no way it can be achieved without exposing the website's port, in your case 7000 .但是,我得出的结论是,如果不公开网站的端口,就无法实现它,在您的情况下7000 I've tried it on IntelliJ PHPStorm .我已经在IntelliJ PHPStormIntelliJ PHPStorm

Reason:原因:

xdebug tries to upload a file _intellij_phpdebug_validator.php to the api container using Path Mapping in order to validate the URL. xdebug尝试使用Path Mapping将文件_intellij_phpdebug_validator.php上传到api容器以验证 URL。 eg it tries to upload the file at var/www/my_api , however, it doesn't find this folder on the nginx_proxy container since this is the only container accessible using that particular URL http://localhost/my-api .例如,它尝试将文件上传到var/www/my_api ,但是,它在nginx_proxy容器上找不到此文件夹,因为这是唯一可以使用该特定 URL http://localhost/my-api访问的容器。 And when it tries to access this file through URL http://localhost/my-api/_intellij_phpdebug_validator.php , the internal proxy pass call tries to access the file /var/www/my_api/_intellij_phpdebug_validator.php and when it doesn't find that file, it returns the error 404 not found .当它尝试通过 URL http://localhost/my-api/_intellij_phpdebug_validator.php访问此文件时,内部代理传递调用尝试访问文件/var/www/my_api/_intellij_phpdebug_validator.php并且当它没有时找到该文件,它返回错误404 not found

In othercase, when you use xdebug by exposing your port 7000 and when you access the URL http://localhost:7000 , it finds the correct Path Mapping ie /var/www/my_api and xdebug starts working alright.在其他情况下,当您通过暴露端口7000使用xdebug并访问 URL http://localhost:7000 ,它会找到正确的路径映射,即/var/www/my_api并且xdebug开始正常工作。

NGINX Proxy container Error Log entry (trying to find the file at /etc/nginx/html ie the server's path whereas it was trying initially to upload the file on /var/www/web as per configuration) NGINX 代理容器错误日志条目(尝试在/etc/nginx/html查找文件,即服务器的路径,而它最初尝试根据配置将文件上传到/var/www/web

2021/10/27 10:50:37 [error] 34#34: *25 open() "/etc/nginx/html/_intellij_phpdebug_validator.php" failed (2: No such file or directory), client: 172.23.0.1, server: proxy, request: "GET /_intellij_phpdebug_validator.php HTTP/1.1", host: "127.0.0.11"

Website container Error Log entry (trying to find the file at /var/www/web ie the website's path whereas it was never uploaded here, thus giving error 404)网站容器错误日志条目(试图在/var/www/web找到文件,即网站的路径,而它从未上传到这里,因此给出错误 404)

172.23.0.2 - - [27/Oct/2021:10:51:01 +0000] "GET /web/_intellij_phpdebug_validator.php HTTP/1.0" 404 16 "-" "Java/11.0.6" "172.23.0.1"

路径映射冲突的图形表示

错误 404

暴露PORT后

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

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