valet: 502 Bad Gateway nginx/1.15.8

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?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 13
  • Comments: 49 (9 by maintainers)

Most upvoted comments

Did you try this

brew services start php

If it didn’t work, try to reinstall php from source

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

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

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 php@7.2

More about it here: Laravel Valet PHP Versions

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

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 php@x.x 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
brew services stop php
brew services start php

valet use php@8.1 --force #(This worked for me)

composer global update

valet park
valet link
valet secure

# Clear browser history

valet use php@8.1 --force This worked for me.

Upgrading to php 7.4 worked for me.

valet use 7.4

valet unistall valet install valet park

these 3 steps worked for me

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

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 php@7.4

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

valet restart
valet parked
valet use php@7.4

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

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

valet use php@7.2 did the trick for me. thanks @drewroberts, you saved me lots of hours 😃

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

brew services stop php brew services start php

$ brew services start php
Bootstrap failed: 5: Input/output error
Error: Failure while executing; `/bin/launchctl bootstrap gui/501 /Users/***/Library/LaunchAgents/homebrew.mxcl.php.plist` exited with 5.
$ brew services stop php
Stopping `php`... (might take a while)
==> Successfully stopped `php` (label: homebrew.mxcl.php)
$ brew services start php
==> Successfully started `php` (label: homebrew.mxcl.php)
$ valet restart
Restarting dnsmasq...
Restarting php...
Restarting nginx...
Valet services have been restarted.

Fixed.

FYI, Just updated to PHP 7.49 and tried using valet. Got: In PhpFpm.php line 212: Valet doesn’t support PHP version: php@7.49 (try something like ‘php@7.3’ instead)

So, is something to take into consideration.

The following commands worked for me.

valet use php@7.2 composer global update

Both @ElectroBuddha and @drewroberts answers did the tricks

Fixed it by doing

brew services start --all

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