首页 > 解决方案 > Ngrok 到 dockerised symfony 5 的隧道以 502 bad gateway 结束

问题描述

我在 docker 容器中有一个全新的 symfony 5 应用程序,我需要在其上接收一些 webhook,从而使用 ngrok 进行隧道传输。问题是,ngrok 不断向我发送 502 错误,而 localhost 访问却做得很好。我有点没有想法了。

使用 localhost 卷曲:

$ curl -v http://localhost:4080/default
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 4080 (#0)
> GET /default HTTP/1.1
> Host: localhost:4080
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Cache-Control: no-cache, private
< Content-Type: application/json
< Date: Thu, 23 Jul 2020 09:10:43 GMT
< PHP message: [info] Matched route "default".
< X-Powered-By: PHP/7.4.3
< X-Robots-Tag: noindex
< Content-Length: 93
< 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
{"message":"Welcome to your new controller!","path":"src\/Controller\/DefaultController.php"}

使用 localhost 时的 Docker 日志:

payment_1                 | [Web Server/PHP ] Jul 23 09:10:43 |INFO | PHP    PHP message: Matched route "default". path="/usr/local/sbin/php-fpm" php="7.4.3"
payment_1                 | [Web Server/PHP ] Jul 23 09:10:43 |DEBUG| PHP     
payment_1                 | [Web Server/PHP ] Jul 23 09:10:43 |INFO | SERVER GET  (200) ]8;;https://127.0.0.1:8004/default\/default]8;;\ ip="172.20.0.1"

Ngrok 开始于:

$ ngrok http 4080
ngrok by @inconshreveable                                                                                                                                                                          (Ctrl+C to quit)
                                                                                                                                                                                                                   
Session Status                online                                                                                                                                                                               
Account                       lulhum (Plan: Free)                                                                                                                                                                  
Version                       2.3.35                                                                                                                                                                               
Region                        United States (us)                                                                                                                                                                   
Web Interface                 http://127.0.0.1:4040                                                                                                                                                                
Forwarding                    http://8c882c861512.ngrok.io -> http://localhost:4080                                                                                                                                
Forwarding                    https://8c882c861512.ngrok.io -> http://localhost:4080                                                                                                                               
                                                                                                                                                                                                                   
Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                                          
                              0       0       0.00    0.00    0.00    0.00

通过 ngrok 卷曲:

$ curl -v http://8c882c861512.ngrok.io/default
*   Trying 3.137.63.131...
* TCP_NODELAY set
* Connected to 8c882c861512.ngrok.io (3.137.63.131) port 80 (#0)
> GET /default HTTP/1.1
> Host: 8c882c861512.ngrok.io
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 502 Bad Gateway
< Content-length: 107
< Cache-Control: no-cache
< Connection: close
< Content-Type: text/html
< 
<html><body><h1>502 Bad Gateway</h1>
The server returned an invalid or incomplete response.
</body></html>
* Curl_http_done: called premature == 0
* Closing connection 0

Docker 通过 ngrok 记录日志(和之前一样):

payment_1                 | [Web Server/PHP ] Jul 23 09:17:07 |INFO | PHP    PHP message: Matched route "default". path="/usr/local/sbin/php-fpm" php="7.4.3"
payment_1                 | [Web Server/PHP ] Jul 23 09:17:07 |DEBUG| PHP     
payment_1                 | [Web Server/PHP ] Jul 23 09:17:07 |INFO | SERVER GET  (200) ]8;;https://127.0.0.1:8004/default\/default]8;;\ ip="172.20.0.1"

Ngrok 控制台(似乎还可以):

HTTP Requests                                                                                                                                                                                                      
-------------                                                                                                                                                                                                      
                                                                                                                                                                                                                   
GET /default                   200 OK 

docker 容器通过 docker compose(docker-compose.yml 提取)与 symfony 开发服务器一起启动:

version: '3'
services:

  payment:
    build: ./payment
    command: sh -c "symfony server:stop && symfony server:start --port=4080"
    environment:
      APP_DEBUG: 1
      VAR_DUMPER_SERVER: payment_dump:8000
    volumes:
      - "./payment:/app"
      - "payment_composer:/root/composer"
    expose:
      - 4080
    ports:
      - "4080:4080"

这是 Dockerfile:

FROM php:7.4-fpm-alpine

RUN apk add --no-cache \
  git \
  curl \
  openssl \
  ssmtp \
  libzip-dev \
  bash

RUN docker-php-ext-install -j$(nproc) \
  pcntl \
  zip

RUN apk add --no-cache icu-dev
RUN docker-php-ext-install intl

RUN wget https://get.symfony.com/cli/installer -O - | bash \
    && mv /root/.symfony/bin/symfony /usr/local/bin/symfony

RUN curl https://raw.githubusercontent.com/composer/getcomposer.org/d3e09029468023aa4e9dcd165e9b6f43df0a9999/web/installer | php -- --quiet \
    && mv composer.phar /usr/local/bin/composer

WORKDIR /app

ENV TZ="Europe/Paris"
ENV IS_DOCKER=1

我真的不明白出了什么问题。我有其他具有类似配置的容器似乎可以正常工作。我尝试使用 *:4080 或http://127.0.0.1:4080或一些 -host-header 选项重建容器和各种 ngrok 命令。还尝试了不同的端口,但没有结果。结果在浏览器中当然是一样的。

标签: phpdockersymfonyngrok

解决方案


我已经缩小了问题的范围。问题出在 symfony 服务器中,添加了奇怪的PHP message: [info] Matched route "default".标头。此消息由记录器发出,应该发送到 stderr。不知道它是如何到达那里的。

由于它不遵循传统的标头语法,我认为 ngrok 无法读取标头并回退到 502。

向 symfony 提出了一个问题,同时使用了基本的 php 服务器。


推荐阅读