Search code examples
nginxgoogle-cloud-platformgoogle-app-engineloggingphp-8.2

Google App Engine 502 (Bad Gateway), but 200 (OK) in log with PHP 8.2


I'm migrating a GCP app engine from an old PHP5.5 to a PHP8.2. The app deploy passes OK. When sending requests to the GAE, I always get HTTP/1.1 502 Bad Gateway errors, but according to the logs it was 200 OK. Therefore adding ini_set('display_errors', 1); error_reporting(E_ALL); does not provide any information. It actually changes nothing, indicating there is no PHP error.

  • It seems that 502 on GCP are usually due to memory issue, but the logs do not report this in my case.

  • I made two requests in the provided logs, the first was booting the instance (799ms) the second (25ms) is confirming the instance is still up and running, meaning it does not crash and GAE didn't terminated the application.

  • The requests completed quite fast, so it doesn't seem to be a computing length issue.

  • Multiples tries have been done over multiples hours, so it doesn't seem to be a timing issue.

I do not understand why it is doing this and how to debug it since I have incoherent logs.

Here my app.yaml:

runtime: php82
app_engine_apis: true
entrypoint: serve --enable-dynamic-workers front-controller.php
handlers:
    ... some handlers
env_variables:
    ... some variables

Here the Response:

HTTP/1.1 502 Bad Gateway
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
connection: close
content-length: 332
content-type: text/html; charset=UTF-8
date: Fri, 22 Dec 2023 12:43:33 GMT
referrer-policy: no-referrer


<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>502 Server Error</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Server Error</h1>
<h2>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds.</h2>
<h2></h2>
</body></html>

Here a screenshot of the logs, in GCP Logs Explorer, showing two requests with success http 200 and no error


Solution

  • Resolved

    I found that adding any not empty string anywhere prior to the actual output of my script (like a var_dump or just an echo " ") is making the request to succeed (HTTP 200), with the expected output, preceded by the echoed string, but with no error. My script is an API, so it builds the request's answer, then calls a function that sets the json header and echoes the output. I therefore thought it must be an headers related issue (as echoing anything prevent further header setting, eventhough I didn't get the usual error related to that)

    I commented each line one by one untill I find that so silly thing:
    It seems that while it was just redundant to set both json and utf8 headers on the previous setup (PHP5.5 AppEngine Gen-1) on PHP8.2 AppEngine Gen-2, it is generating a 502 error respond while logging a 200 OK.

    So I just removed the 'charset=utf-8' header and it is now working fine.

    Anyway I still think that the mismatch of the output and the log is a bug on GAE.

    Hoping this could help others