Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skye1985's avatar

502 Bad Gateway on certain API Calls

I'm encounter an error on my laravel installations on my computer. On certain calls (with payloads, with bearer token, ecc.) i recieve a "502 Bad Gateway" error. This is my configuration:

MacOS Valet 4.3.0 PHP 8.0 Laravel 8.12

If i create a test route with a simple string print api call works. But if i use standard call with Request and Response nginx of Valet log this error:

[error] 6601#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: ...

I didn't change anythings of my laravel source code in the meanwhile, but probably changed somethings in my configuration. Anyone can help me to suggest some other checks?

Thanks in advance

0 likes
9 replies
skye1985's avatar

Thanks a lot for your time, @katsry. I just tried all your precious suggestions and tips, but unfortunatly the problems still occured:

  • I checked my valet installation (valet uninstall, valet install, tried "Fix My Valet" );
  • I checked php-fpm, php and laravel logs but nothings logged;
  • I checked nginx log and my only message is the same i had posted in my first message;
  • I increased both php memory limit and fastcgi buffer;
  • all databases and websites in valet parked folder works;
  • if i creare a simple laravel enpoint with a simple "test" string printed and it works

But still every api call in resource crud actions in every laravel local instance in my computer respond with a 502 error.

skye1985's avatar

I probably found the moment when I starting got this error: when I installed [email protected] via "brew install php". This command install php 8.2.x and a lot of packages. This is because I found the App fails when attempt to login and a php package stuck somewhere (I googled a lot so this is a supposition...). Do you know how to remove php 8.2 and all its packages? I usually use 8.0.x and php.7.4.x for older projects.

skye1985's avatar

An update: after restarting my computer the problem returns... I'm trying to find the reason.

skye1985's avatar

UPDATE: I tried to completly uninstall and reinstall valet and php, but the issue still remains.

My env setup is:

  • Valet 4.3.0
  • PHP 8.0
  • Homebrew 4.1.13
  • nginx 1.25.2

Any suggestions?

skye1985's avatar

Ok, found where app crashes in a 502 error. When I perform an Eloquent function. So it's limited to those actions. So, to recap my situation:

  • My Laravel version is 8.12.
  • If I try the same function (e.g.: Card::find(1)) in thinker session it works perfectly
  • if a try a test API call (eg.: a simple test call with a string as response) it works perfectly
  • if I navigate su app via browser it works perfectly
  • if I try a Api call to a function of a controller where I have to perform an Eloquent query (e.g.: Card::find(1)) it responds with a 502 Bad Gateway error (with Nginx log "upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server:").

How can I resolve?

skye1985's avatar

@kerranm unfortunately not yet... it's very frustrating, because in computer where I din't update composer, Nginx and valet, everything works yet... and in other computer can't resolve, also trying to downgrade to older versions of each packages of the stack...

skye1985's avatar
skye1985
OP
Best Answer
Level 1

I finally found a solution to my problem, and I want to share to anyone can be fall to this pit (@kerrnm):

In /usr/local/etc/php/8.2/php-fpm.d/www.conf after the following:

; Pass environment variables like LD_LIBRARY_PATH. All $VARIABLEs are taken from
; the current environment.
; Default Value: clean env
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp

Add an environment variable like this:

env['PGGSSENCMODE'] = disable

Then restart php. This will work even after a reboot.

Please or to participate in this conversation.