Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

502 Bad Gateway - nginx/1.10.2 #269

Closed
rsm23 opened this issue Dec 17, 2016 · 76 comments
Closed

502 Bad Gateway - nginx/1.10.2 #269

rsm23 opened this issue Dec 17, 2016 · 76 comments

Comments

@rsm23
Copy link

@rsm23 rsm23 commented Dec 17, 2016

I'm using Laravel Valet 2.0.3 on MacOS Sierra
And I'm getting this error everytime I restart my laptop

screen shot 2016-12-17 at 10 41 41 am

@adamwathan

This comment has been minimized.

Copy link
Member

@adamwathan adamwathan commented Dec 17, 2016

@rsm23

This comment has been minimized.

Copy link
Author

@rsm23 rsm23 commented Dec 17, 2016

Yes of course

@ekandreas

This comment has been minimized.

Copy link

@ekandreas ekandreas commented Dec 19, 2016

Same here...

@rsm23

This comment has been minimized.

Copy link
Author

@rsm23 rsm23 commented Dec 22, 2016

Isn't there any solution for that?

@mpociot

This comment has been minimized.

Copy link

@mpociot mpociot commented Dec 23, 2016

Same for me - I encountered this issue only after updating to Sierra 10.12.2 (16C67).

@ekandreas

This comment has been minimized.

Copy link

@ekandreas ekandreas commented Dec 23, 2016

So, I did work around this by two actions:

  1. Reinstall with the http2 parameter.
  2. Adjust fastcgi buffer settings in valet.conf.

Take a look at some similar issues in this repo!

@mpociot

This comment has been minimized.

Copy link

@mpociot mpociot commented Dec 23, 2016

That's not working for me.
I don't always get a 502 Bad Gateway, I get it 2-3 requests on my .dev sites if I keep refreshing.

So it also happens for static files.

My nginx-error.log file is full of these messages:

2016/12/23 11:41:51 [error] 50831#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/Users/marcel/.valet/valet.sock:", host: "botman-docs.dev"
@hsleewis

This comment has been minimized.

Copy link

@hsleewis hsleewis commented Jan 5, 2017

Also still an issue here,.. anyone has a working solution?

@adamwathan

This comment has been minimized.

Copy link
Member

@adamwathan adamwathan commented Jan 5, 2017

I haven't been able to reproduce this at all but some things online point to it maybe having something to do with large responses and big files. Is anyone able to create some sort of repository or something that demonstrates this in a reproducible way?

@hsleewis

This comment has been minimized.

Copy link

@hsleewis hsleewis commented Jan 5, 2017

I have a couple of pages where I do a number of external requests, it looks like I get the error on those pages. Unfortunately nothing I can make available publicly. I even reinstalled my new MacBook Pro on a new partition and tested it, but I get exactly the same issues.

@simonpeters

This comment has been minimized.

Copy link

@simonpeters simonpeters commented Jan 6, 2017

Having the same issue since updating to 2.0.3

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Jan 6, 2017

What's recorded in your ~/.valet/Log/nginx-error.log ?

@simonpeters

This comment has been minimized.

Copy link

@simonpeters simonpeters commented Jan 6, 2017

It's an empty file

In error.log I have this though

06/Jan/2017:20:18:47 +0100 [ERROR 502 /server.php] dial tcp 127.0.0.1:9000: getsockopt: connection refused

edit: Rebooting fixed it for me, I think some old service was still running and it was breaking the nginx.

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Jan 6, 2017

Ya, that was gonna be my next comment:

502 Bad Gateway often means that the call to php-fpm is failing ... usually because it hasn't started properly, or the nginx config is pointing to a php-fpm config that's incorrect.

Common configurations for calling php-fpm are:

  • unix socket pointed at unix:/Users/your_name/.valet/valet.sock
  • port 9000, such as 127.0.0.1:9000

On a fresh install of valet, or a fresh upgrade, usually just rebooting is enough, since other services might have been hanging on those and preventing them from starting properly.

Often you can determine this by inspecting your ~/.valet/Log/nginx-error.log and /usr/local/var/log/php-fpm.log
after running sudo brew services restart nginx && sudo brew services restart php71

Glad you got it working.

@tlaverdure

This comment has been minimized.

Copy link
Contributor

@tlaverdure tlaverdure commented Jan 11, 2017

@adamwathan I was getting this error just now when trying to perform a POST. In my ngnix-error.log I'm seeing this error:

 upstream sent too big header while reading response header from upstream

I was able to fix by adding these two lines to my valet.conf file in the location ~ \.php$ block:

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

I also tested this POST request with less form fields and it seems to be consistent with the amount of data being submitted.

Reference

@GiampaoloFalqui

This comment has been minimized.

Copy link

@GiampaoloFalqui GiampaoloFalqui commented Jan 16, 2017

I keep having the same issues.

fastcgi buffer settings did not fix the issue.
rebooting did not fix the issue.

@hsleewis

This comment has been minimized.

Copy link

@hsleewis hsleewis commented Jan 18, 2017

With my computer it always happens when doing a POST request.

@jesusamezag

This comment has been minimized.

Copy link

@jesusamezag jesusamezag commented Jan 21, 2017

I've the same issue...

Here is my log:

2017/01/20 17:59:21 [crit] 14300#0: *196 connect() to unix:/Users/jesusarturomezageraldo/.valet/valet.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/Users/jesusarturomezageraldo/.valet/valet.sock:", host: "foobar.dev" 2017/01/20 17:59:21 [crit] 14300#0: *196 connect() to unix:/Users/jesusarturomezageraldo/.valet/valet.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://unix:/Users/jesusarturomezageraldo/.valet/valet.sock:", host: "foobar.dev", referrer: "http://foobar.dev/"

@jpmurray

This comment has been minimized.

Copy link

@jpmurray jpmurray commented Jan 22, 2017

I'm having the same log output as @jesusamezag here, but it all started to happen after I did valet unsecure on one of my site/folder...

@jpmurray

This comment has been minimized.

Copy link

@jpmurray jpmurray commented Jan 22, 2017

I made a couple uninstall/install cycle and I wouldn't get valet started anymore. What got me back on track was stopping valet, uninstalling nginx and reinstalling it with brew install nginx --with-http2. Now all seems ok.

I don't think it could help for everyone, but in case it could... !

@railto

This comment has been minimized.

Copy link

@railto railto commented Jan 27, 2017

Having same issues and have tried all the solutions in this thread. content from the error log is

2017/01/27 10:39:15 [crit] 41134#0: *7 connect() to unix:/Users/mark/.valet/valet.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/Users/mark/.valet/valet.sock:", host: "personalsite.dev

Hoping someone else has a solution for this as currently means I'm back to using vagrant for local dev and would really rather use valet

Update

I've just managed to resolve this, appears a composer global update followed by valet install is all that was needed, hope that this helps someone else.

@thinkstylestudio

This comment has been minimized.

Copy link

@thinkstylestudio thinkstylestudio commented Jan 27, 2017

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Jan 27, 2017

The valet.sock file has no content. In some ways it's not really even a "file". It's a socket. But it shows up as a file on the filesystem.

The ~/.valet/valet.sock file is created upon PHP startup, and removed on normal shutdown.

If it doesn't exist then PHP didn't/couldn't start properly. Look at the /usr/local/var/log/php-fpm.log for messages.

@xboston

This comment has been minimized.

Copy link

@xboston xboston commented Jan 27, 2017

Hi @rsm23!

Check please:

$ ls -lm $HOME/.valet
Certificates, Drivers, Extensions, Log, Nginx, Sites, config.json, dnsmasq.conf

$ brew services start php71
==> Successfully started `php71` (label: homebrew.mxcl.php71)

$ ls -lm $HOME/.valet
Certificates, Drivers, Extensions, Log, Nginx, Sites, config.json, dnsmasq.conf, valet.sock

Done.

@thomas-oo

This comment has been minimized.

Copy link

@thomas-oo thomas-oo commented Jan 31, 2017

@xboston

I'm not seeing valet.sock after I run brew services start php71. After I start php71 and I check brew services list, it is started.

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Jan 31, 2017

Have you tried sudo instead?

@thomas-oo

This comment has been minimized.

Copy link

@thomas-oo thomas-oo commented Jan 31, 2017

yes now I see valet.sock and my issue is no long a 502 error but now I have a 404 error, valet - not found... and yes I've valet park'ed the directory above the valet project I've created

edit: nvm I'm dumb I had another domain (not .dev) for valet domain and forgot.

@tatocaster

This comment has been minimized.

Copy link

@tatocaster tatocaster commented Jan 31, 2017

after upgrading to 2.XX i have the same problem.
usr/local/var/log/php-fpm.log is empty,
nginx-error.log says .valet/valet.sock failed (2: No such file or directory)

@tatocaster

This comment has been minimized.

Copy link

@tatocaster tatocaster commented Jan 31, 2017

sudo brew services restart nginx && sudo brew services restart php71
if it writes that permissions are messed up , use this link below.
StackOverflow

everything is up and running.

@ZeoKnight

This comment has been minimized.

Copy link

@ZeoKnight ZeoKnight commented Feb 3, 2017

@tatocaster thanks - that has put it right for me seems the nginx/1.10.3/homebrew.mxcl.nginx.plist had permission issues - that SO post helped

@llbbl

This comment has been minimized.

Copy link

@llbbl llbbl commented Dec 29, 2017

Hey everyone. The solution this is to that nginx can't pass requests to php-fpm. This is normally because php-fpm listener is not configured correctly. PHP can be configured to respond to request via unix socket or via TCP socket.

Check where your www.conf file is configured to listen:

listen = /run/php/php7.0-fpm.sock
or
listen = 127.0.0.1:9000
or what valet wants... 
listen = /Users/YourUserName/.valet/valet.sock

Hope this helps someone else. Just finished double clawing myself outta that hole.

The config files you care about are here:

/usr/local/etc/php/7.2/php-fpm.d/www.conf
~/valet/Nginx/sitename

The nginx conf file will only show up if you do a valet secure, otherwise not sure where it is.

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Dec 29, 2017

True.
If I recall correctly, an older version of Valet formerly listened on 127.0.0.1:9000.
I ran into this when I had an older version of PHP installed, then installed a newer version, then ran valet install, and it only updated the PHP config for the newer PHP.
Then when attempted to switch back to older PHP version it was looking on localhost instead of the socket.
Updating the www.conf (or equiv) for the various PHP versions I have available solved all the conflicts.

@cheerego

This comment has been minimized.

Copy link

@cheerego cheerego commented Jan 23, 2018

I changed my php version from 7.2 to 7.1 ,found this error.
I solve it via ‘valet install’

@same07

This comment has been minimized.

Copy link

@same07 same07 commented Jan 29, 2018

@tlaverdure thx bro.. u save my day...

adding

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

works well..

@raveenasurani

This comment has been minimized.

Copy link

@raveenasurani raveenasurani commented Apr 6, 2018

Hello guys,

I was having the same issue after knocking my head around i came upto this solution and its working perfectly .. Here you go 👍

so Firstly, i install Brew : /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then i installed php71 which is now php@7.1 : brew install php71

-> after installation export : echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.bash_profile

-> brew link php@7.1 --force --overwrite

->ln -s $(which php) /usr/local/bin/php

Then i installed composer using :

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

and mv composer.phar /usr/local/bin/composer

and then installed valet : composer global require laravel/valet

And then comes the tricky part :

-> open -e /Users/USERNAME-Directory/.composer/vendor/laravel/valet/cli/Valet/Brew.php

and alter

function supportedPhpVersions()
{
return collect(['php', 'php72', 'php71', 'php70', 'php56','php@7.1']);
}

-> open -e /Users/USERNAME-Directory/.composer/vendor/laravel/valet/cli/Valet/PhpFpm.php

and alter

function fpmConfigPath()
{
$confLookup = [
'php' => '/usr/local/etc/php/7.2/php-fpm.d/www.conf',
'php72' => '/usr/local/etc/php/7.2/php-fpm.d/www.conf',
'php71' => '/usr/local/etc/php/7.1/php-fpm.d/www.conf',
'php70' => '/usr/local/etc/php/7.0/php-fpm.d/www.conf',
'php56' => '/usr/local/etc/php/5.6/php-fpm.conf',
'php@7.1' => '/usr/local/etc/php/7.1/php-fpm.d/www.conf'
];

return $confLookup[$this->brew->linkedPhp()];

}
and then final step : sudo brew services restart nginx && sudo brew services restart php@7.1

And DONE!!!!

Cheers!!

@zhaochunqi

This comment has been minimized.

Copy link

@zhaochunqi zhaochunqi commented Apr 10, 2018

It seems like the php-fpm not bundled when use brew install php.

I did this:

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

Finally, it works, hopes it help.

@hsleewis

This comment has been minimized.

Copy link

@hsleewis hsleewis commented Apr 17, 2018

@tlaverdure do you know if there is a way to automatically add the fastcgi settings to all valet hosts?

@drbyte

This comment has been minimized.

Copy link
Contributor

@drbyte drbyte commented Apr 17, 2018

@hsleewis wrote:

do you know if there is a way to automatically add the fastcgi settings to all valet hosts?

Running valet domain test will do this. First it will set the TLD for Valet to .test, and then it will rebuild every nginx config so that it uses that TLD. It uses the valet.conf and secure.valet.conf stubs in your ~/.composer/vendor/laravel/valet/cli/stubs directory as the templates for rebuilding those configs.
So if you want to change all your hosts configs, simply update the stubs and run valet domain test.

Then restart nginx with valet restart or with brew services nginx restart

@ilterocal

This comment has been minimized.

Copy link

@ilterocal ilterocal commented Apr 29, 2018

brew commands should be run with "sudo" otherwise it fails to create the valet.sock in valet folder.

@acymru

This comment has been minimized.

Copy link

@acymru acymru commented May 4, 2018

When trying to run brew commands as "sudo":
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

@Sauruz

This comment has been minimized.

Copy link

@Sauruz Sauruz commented May 8, 2018

I've tried everything above and none seemed to work. Until I noticed I had 2 php versions in /usr/local/etc/php. Deleting both and doing all the steps at https://laravel.com/docs/5.6/valet solved my problem. It took me quite some hours of pulling my hair out.

@gindra

This comment has been minimized.

Copy link

@gindra gindra commented May 25, 2018

I have same problem php-fpm not loaded, because of new brew sudo requirements, brew reinstall php71 --build-from-source did not have permissions to create necessary folders for fpm, so I gave 777 permission to the PHP folder and run reinstall php71 --build-from-source, restart pc and, run vallet install and everything works great.

@kevinruscoe

This comment has been minimized.

Copy link

@kevinruscoe kevinruscoe commented May 29, 2018

My issue was that the sock file was not created. I installed php@7.1 from the core homebrew repo.

My solution was to edit /usr/local/etc/php/7.1/php-fpm.d/www.conf to start as my user and the staff group. Then amend /usr/local/etc/nginx/valet/valet.conf and change fastcgi_pass unix:/Users/me/.valet/valet.sock; to fastcgi_pass 127.0.0.1:9000;. Homebrew always lists php-fpm as running php@7.1 started root /Library/LaunchDaemons/homebrew.mxcl.php@7.1.plist but I dunno why the sock doesn't get created. Perhaps it's a permission issue.

@bradycharron-sephone

This comment has been minimized.

Copy link

@bradycharron-sephone bradycharron-sephone commented Jun 14, 2018

I have come up against this issue when trying to configure my database with SSL, and connect to it with the MySql driver. Per a few different resources, I found the following configuration for the MySql driver to add support for SSL connections, but with this driver configuration, I get the Bad Gateway error.

Edit: change array format on options array, did not produce any positive results.

Driver:

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'sslmode' => env('DB_SSLMODE', 'prefer'),
            'options'   => [
                PDO::MYSQL_ATTR_SSL_CA    => '/path/to/file/ca.pem',
                PDO::MYSQL_ATTR_SSL_CERT    => '/path/to/file/client-cert.pem',
                PDO::MYSQL_ATTR_SSL_KEY    => '/path/to/file/client-key.pem'
            ],
            'strict' => true,
            'engine' => null,
        ],

I am using Laravel Valet as the development environment for this. I get the following error in the Valet NGINX error log: 2018/06/14 07:51:50 [error] 6495#0: *15 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: ccc.dev, request: "POST /oauth/token HTTP/1.1"

Does anyone know how to resolve this issue with a SSL enabled DB connection?

@trungtnm

This comment has been minimized.

Copy link

@trungtnm trungtnm commented Aug 25, 2018

@drbyte You saved my day!
PHP-FPM complains about some missing images when I have looked at /usr/local/var/log/php-fpm.log . So i run brew update && brew upgrade php@7.1 , then everything works fine.

@kundaneyug

This comment has been minimized.

Copy link

@kundaneyug kundaneyug commented Dec 12, 2018

This worked for me
composer global update
valet install
valet restart

@jorshhh

This comment has been minimized.

Copy link

@jorshhh jorshhh commented Dec 27, 2018

This worked for me
composer global update
valet install
valet restart

This worked for me too

@tammai

This comment has been minimized.

Copy link

@tammai tammai commented Dec 31, 2018

This worked for me
composer global update
valet install
valet restart

Me too

@hezll

This comment has been minimized.

Copy link

@hezll hezll commented Jan 13, 2019

valet install after switch PHP version

@AbsohAbsoh

This comment has been minimized.

Copy link

@AbsohAbsoh AbsohAbsoh commented Jan 26, 2019

Make sure PHP is running.

brew services start php

@akilsagitarius

This comment has been minimized.

Copy link

@akilsagitarius akilsagitarius commented Jan 28, 2019

This worked for me
composer global update
valet install
valet restart

This worked too me

@CptMeatball

This comment has been minimized.

Copy link

@CptMeatball CptMeatball commented Jan 30, 2019

Also running into this problem after updating brew, php and valet. I was still running on php71 and after deleting all php versions and installed php 7.3 with the php@7.3 command.

All seemed fine, updated MacOS and running into the 502 now.

Will try and report what worked for me.

@AbsohAbsoh

This comment has been minimized.

Copy link

@AbsohAbsoh AbsohAbsoh commented Jan 30, 2019

@CptMeatball make sure you're actually running PHP with...

brew services start php

If that doesn't work then make sure you're actually running the brew php not one that comes installed on the Mac by default.

which php
@CptMeatball

This comment has been minimized.

Copy link

@CptMeatball CptMeatball commented Jan 30, 2019

This returns

/usr/local/bin/php

Already linked and unlinked php. Something more I should do?

Edit: Misread your initial post. Brew says the php service is already running

Update:
Okay, figured out it has something to do with php 7.3. When installing php@7.2 it seems to work. Next to that rebooting seems to cause the problem again,

Update 2:
Turns out just running brew install php seems to be the culprit. Some way it doesn't install correctly, and it gives the 502. I got it wokring with 7.3 by doing the following

- Uninstall php and valet
$ brew uninstall php
$ brew uninstall valet

- Update nginx ( 1.15.8 for me)
$ brew upgrade nginx

- Install php 7.3
$ brew install php@7.3 --build-from-source

- Install valet
composer global require laravel/valet

If it still not works try one of the following:

- Rerun TLD configs
$ valet tld test

- Unlink and link php
$ brew unlink php
$ brew link php@7.3 --force
@HPWebdeveloper

This comment has been minimized.

Copy link

@HPWebdeveloper HPWebdeveloper commented Feb 6, 2019

I had such experience and solved it finally. The solution is here

@whoacowboy

This comment has been minimized.

Copy link

@whoacowboy whoacowboy commented Apr 12, 2019

I tried everything on this chain and then some to no avail.

It turns out that this file /usr/local/etc/php/7.3/php-fpm.d/www.conf was empty.

I copied www.conf.default to www.conf and restarted valet and everything is now working.

cd /usr/local/etc/php/7.3/php-fpm.d/
cp www.conf.default www.conf
valet restart
@ihsanberahim

This comment has been minimized.

Copy link

@ihsanberahim ihsanberahim commented Jun 10, 2019

after upgrading to 2.XX i have the same problem.
usr/local/var/log/php-fpm.log is empty,
nginx-error.log says .valet/valet.sock failed (2: No such file or directory)

i do sudo touch /usr/local/var/log/nginx/access.log then it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.