Valet: 502 Bad Gateway nginx/1.15.8

Created on 28 Jan 2019  路  37Comments  路  Source: laravel/valet

I'm getting a 502 Bad Gateway nginx/1.15.8 error when trying to enter a .test site. This is on a completely fresh Mojave installation where I just installed with Homebrew php (7.3.1), nginx (1.15.8) (among others) and Valet (2.1.6) through composer. I haven't modified any default configuration of any of the aforementioned packages. I have run valet install and valet park in the folder where my project is at.

Any idea why this is happening?

Most helpful comment

I ran into this issue as well. After doing the commands shared in the other responses, I also had to change back to php 7.2 instead of 7.3 and here's that command:

valet use [email protected]

More about it here: Laravel Valet PHP Versions

All 37 comments

@Flerex 7.3 has been almost unusable; I suggest moving to 7.2.

Hmmm ... I've had no issues with 7.3 on a fresh Mojave install.

Maybe I'll wipe this new machine and test again.

@mattdfloyd 7.2 installed, same thing happening.

As I understand it, Bad Gateway means Nginx isn't finding the PHP-FPM service to proxy (gateway) the request through.

Common causes:

  • PHP isn't running
  • the valet config isn't finding the valet.sock socket, which is created by the PHP process when it starts
  • the valet nginx configs are pointing to the wrong php version

Did you try this

brew services start php

If it didn鈥檛 work, try to reinstall php from source

brew uninstall php
brew install php --build-from-source
valet install

It worked! Thanks @florianbouvot

I know this is closed but i had the same issue and i was because some how brew had running an older php version so:
sudo brew services cleanup
and after i restart it worked. i have Mac OSX High Sierra btw

A simple restart of the brew services with sudo in front and an adjacent restart of valet did the job for me:

sudo brew services restart nginx && sudo brew services restart php
valet restart

I ran into this issue as well. After doing the commands shared in the other responses, I also had to change back to php 7.2 instead of 7.3 and here's that command:

valet use [email protected]

More about it here: Laravel Valet PHP Versions

Happened to me as well after i upgraded my macos with brew update && brew upgrade

valet use [email protected] did the trick for me. thanks @drewroberts, you saved me lots of hours :)

I ran brew upgrade && brew update && brew cleanup recently and it upgraded me from 7.3.5 to 7.3.6 and is working fine.
It's been awhile since I bothered with PHP 7.2, so maybe your glitch was in moving from 7.2 to 7.3.

The Bad Gateway message means Nginx can't find a running PHP instance, as determined by having PHP-FPM properly configured to create the valet.sock file so Nginx can use that to send PHP request through. The default valet configs for PHP-FPM work fine on my machine.
If yours aren't working, you can delete your /usr/local/etc/php directory (backups first of course) and then re-install 7.3.

Happened to me as well after i upgraded my macos with brew update && brew upgrade

valet use [email protected] did the trick for me. thanks @drewroberts, you saved me lots of hours :)

I cant use the "valet use" it says command use not defined

I cant use the "valet use" it says command use not defined

That suggests you're using an old version of Valet. I believe the use command was added in 2.2.0

Fixed it by doing

brew services start --all

a really weird reason that solved it was a missing default value just as i passed it the POST method worked

Started having this issue after I updated php to v7.3.
Tried all of the suggestions, fixed only after I updated valet to the latest version (v2.3.3):
composer global update
valet install

Both @ElectroBuddha and @drewroberts answers did the tricks

Started having this issue after I updated php to v7.3.
Tried all of the suggestions, fixed only after I updated valet to the latest version (v2.3.3):
composer global update
valet install

Thank you, it worked

valet unistall
valet install
valet park

these 3 steps worked for me

Started having this issue after I updated php to v7.3.
Tried all of the suggestions, fixed only after I updated valet to the latest version (v2.3.3):
composer global update
valet install

Thank you. it worked

Seems every time I update/upgrade brew I have this issue (nginx 1.17.7). Had it again when going to PHP 7.4. Here is what I had to do, not sure if the last thing I did would have worked first, so, the last thing I did was:

valet use [email protected]

Before that, I completely eradicated PHP (note, at least on Catalina, you cannot use sudo in front of brew, or I do not know how):

brew doctor
brew services cleanup
brew list | grep php
brew uninstall --force php@[INSERT VERSIONS]

Re-ran brew doctor and brew services cleanup and brew cleanup and had to manually sudo rm -R [directories] where directories were in usr\local\etc\ then

brew install [email protected] valet restart valet parked valet use [email protected]

Brew is such a PITA. I had to chown a number of directories to fix permissions, rm stuff etc.

FYI - "502 Bad Gateway" from Nginx means the PHP proxy is not running, or not found. (Nginx proxies PHP calls to php-fpm by means of the directives in the nginx site conf files.)

So, if you've updated your PHP to a new non-minor version (ie: 7.2 up to 7.3, or 7.4 down to 7.3) by just running a brew upgrade command, then you'll need to also run valet install to configure that PHP version's configs for Valet. Otherwise your newly-installed PHP version's configs won't know anything about Valet.

If you upgrade PHP or switch to new PHP version via the valet use [email protected] command then Valet will also do the necessary config updates as part of that switch.

My usual workflow (and I normally only use the latest PHP version anyway) is simply the following on a weekly basis:

  • composer global update
  • brew upgrade
    and if homebrew gave me a new major PHP version (ie: 7.4 when I previously had 7.3) then I also run:
  • valet install

Did you try this

brew services start php

If it didn鈥檛 work, try to reinstall php from source

brew uninstall php
brew install php --build-from-source
valet install

it worked

@florianbouvot

I'm getting this 502 error鈥攂ut only when a 500 error would have otherwise been produced. (So no Ignition error page, which is sad.) None of these suggestions are working for me.

I'm getting this 502 error鈥攂ut only when a 500 error would have otherwise been produced. (So no Ignition error page, which is sad.) None of these suggestions are working for me.

If a 500 error is occurring behind the scenes, then you'll need to go back to old-school troubleshooting by consulting the logs. You mentioned Ignition which suggests you're working with a Laravel app, which means your logs will be in Laravel's usual log location.

I'm getting this 502 error鈥攂ut only when a 500 error would have otherwise been produced. (So no Ignition error page, which is sad.) None of these suggestions are working for me.

If a 500 error is occurring behind the scenes, then you'll need to go back to old-school troubleshooting by consulting the logs. You mentioned Ignition which suggests you're working with a Laravel app, which means your logs will be in Laravel's usual log location.

Thanks drbyte. Yes I'm using the latest version of Laravel and Valet. It's just that this is previously unseen behavior with standard PHP errors, and happened after I did some PHP/Homebrew updating. Routes without errors are working fine.

Thanks @rcordobar19 That fixed it.

The following commands worked for me.

valet use [email protected]
composer global update

Upgrading to php 7.4 worked for me.

valet use 7.4

None of these solutions worked for me, and am just about at my wit's end. I want so bad to use valet but the nginx/php-fpm issue is quite clearly a main problem that needs to be fixed. I understand that php-fpm isn't running or getting setup correctly, but the feedback the commands are giving me is not enough to figure out the why, and as mentioned - no solutions here have worked. Any ideas how to debug/resolve?

Hey @kirkbushell , do you mind opening a new issue, and posting the output of valet diagnose as part of it? That'll help with ruling things out as well as potentially identifying related factors that might not be obvious enough yet.

@drbyte actually it's funny, right as I was about to give up, I found an article that helped me resolve it - and it all came down to path export in zshrc.

@kirkbushell I'm glad you got it sorted out. If you think the article points to something that's not rather unique to your situation but rather is likely to be common for others to benefit from, feel free to share the link when you have a minute.

Cheers!

For me, it was this article that set me straight:

https://programmer.help/blogs/5e2800f291b7a.html

Specifically, the point about paths in zshrc.

FYI,
Just updated to PHP 7.49 and tried using valet. Got:
In PhpFpm.php line 212:
Valet doesn't support PHP version: [email protected] (try something like '[email protected]' instead)

So, is something to take into consideration.

In my case php was unlinked but already installed so running brew link [email protected] --force and restarting valet did the trick

Was this page helpful?
0 / 5 - 0 ratings