laravel / valet

A more enjoyable local development experience for Mac.
https://laravel.com/docs/valet
MIT License
2.52k stars 698 forks source link

502 Bad Gateway errors when making external requests (when libvips is installed) #1085

Closed ryangjchandler closed 2 years ago

ryangjchandler commented 3 years ago

Clear description of your problem

When making requests to an external service, e.g. an API all request result in a 502 Bad Gateway error.

Sites run fine otherwise, it's only when making external requests. E.g. Stripe requests fail, even requests from one local site to another.

Expected behavior

Requests should not fail.

Steps to Reproduce

Really not sure on the reproduction steps. It started happening out of the blue.

Diagnosis

sw_vers
ProductName:   macOS
ProductVersion: 11.4
BuildVersion:   20F71
valet --version
Laravel Valet 2.16.0
cat ~/.config/valet/config.json
{
    "tld": "test",
    "loopback": "127.0.0.1",
    "paths": [
        "/Users/ryan/.config/valet/Sites"
    ]
}
cat ~/.composer/composer.json
{
    "require": {
        "laravel/installer": "^4.1",
        "laravel/valet": "^2.13",
        "nunomaduro/laracon-schedule": "^1.7",
        "cpx/cpx": "^0.1.3",
        "beyondcode/expose": "2.0",
        "laravel-zero/installer": "^2.6",
        "themsaid/ibis": "^0.1.4",
        "schmidfelix/ploi-cli": "^1.3",
        "laravel/forge-cli": "^1.0"
    }
}
composer global diagnose
Changed current directory to /Users/ryan/.composer
Checking composer.json: WARNING
No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
require.beyondcode/expose : exact version constraints (2.0) should be avoided if the package follows semantic versioning
Checking platform settings: OK
Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys: FAIL
Missing pubkey for tags verification
Missing pubkey for dev verification
Run composer self-update --update-keys to set them up
Checking composer version: OK
Composer version: 2.1.5
PHP version: 8.0.9
PHP binary path: /opt/homebrew/Cellar/php/8.0.9/bin/php
OpenSSL version: OpenSSL 1.1.1k  25 Mar 2021
cURL version: 7.78.0 libz 1.2.11 ssl (SecureTransport) OpenSSL/1.1.1k
zip: extension present, unzip present, 7-Zip not available
composer global outdated
Changed current directory to /Users/ryan/.composer
Legend:
! patch or minor release available - update recommended
~ major release available - update possible
beyondcode/expose             2.0.0  ! 2.0.2  Create public URLs for local sites through any firewall and VPN.
league/commonmark             1.6.6  ~ 2.0.1  Highly-extensible PHP Markdown parser which fully supports the CommonMark spec and Github-Flavored Markdown (GFM)
padraic/phar-updater          v1.0.6 = v1.0.6 A thing to make PHAR self-updating easy and secure.
Package padraic/phar-updater is abandoned, you should avoid using it. No replacement was suggested.
psr/container                 1.1.1  ~ 2.0.1  Common Container Interface (PHP FIG PSR-11)
psr/log                       1.1.4  ~ 3.0.0  Common interface for logging libraries
spatie/commonmark-highlighter 2.1.1  ~ 3.0.0  Highlight your markdown code blocks with league/commonmark
ls -al /etc/sudoers.d/
total 16
drwxr-xr-x   4 root  wheel   128  5 Jun 14:29 .
drwxr-xr-x  86 root  wheel  2752 12 Aug 12:39 ..
-rw-r--r--   1 root  wheel    83  2 Jun 17:42 brew
-rw-r--r--   1 root  wheel    86  2 Jun 17:42 valet
brew config
HOMEBREW_VERSION: 3.2.6
ORIGIN: https://github.com/Homebrew/brew
HEAD: 265c8c36a4c0a74f478cba16417940b3b588736c
Last commit: 9 days ago
Core tap ORIGIN: https://github.com/Homebrew/homebrew-core
Core tap HEAD: 511599504f97e45976b5fafbf5f5c4ccdce2c267
Core tap last commit: 34 minutes ago
Core tap branch: master
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.3 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit arm_firestorm_icestorm
Clang: 12.0.5 build 1205
Git: 2.30.1 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 11.4-arm64
CLT: 12.5.0.22.9
Xcode: 12.5.1
Rosetta 2: false
brew services list
Name    Status  User Plist
dbus    unknown      
dnsmasq unknown      
mailhog started ryan /Users/ryan/Library/LaunchAgents/homebrew.mxcl.mailhog.plist
nginx   unknown      
php     unknown      
unbound unknown
brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"
dnsmasq 2.85
mailhog 1.0.1
nginx 1.21.1
openssl@1.1 1.1.1k
php 8.0.9
brew outdated
node
unbound
brew tap
armmbed/formulae
homebrew/cask
homebrew/cask-fonts
homebrew/core
homebrew/services
minio/stable
nicoverbruggen/cask
stripe/stripe-cli
php -v
PHP 8.0.9 (cli) (built: Jul 29 2021 08:52:24) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.9, Copyright (c) Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
    with Zend OPcache v8.0.9, Copyright (c), by Zend Technologies
which -a php
/opt/homebrew/opt/php/bin/php
/opt/homebrew/bin/php
/usr/bin/php
php --ini
Configuration File (php.ini) Path: /opt/homebrew/etc/php/8.0
Loaded Configuration File:         /opt/homebrew/etc/php/8.0/php.ini
Scan for additional .ini files in: /opt/homebrew/etc/php/8.0/conf.d
Additional .ini files parsed:      /opt/homebrew/etc/php/8.0/conf.d/error_log.ini,
/opt/homebrew/etc/php/8.0/conf.d/ext-opcache.ini,
/opt/homebrew/etc/php/8.0/conf.d/php-memory-limits.ini
nginx -v
nginx version: nginx/1.21.1
curl --version
curl 7.64.1 (x86_64-apple-darwin20.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.41.0
Release-Date: 2019-03-27
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS GSS-API HTTP2 HTTPS-proxy IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets
php --ri curl
curl

cURL support => enabled
cURL Information => 7.78.0
Age => 9
Features
AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => Yes
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => Yes
SPNEGO => Yes
SSL => Yes
SSPI => No
TLS-SRP => Yes
HTTP2 => Yes
GSSAPI => Yes
KERBEROS5 => Yes
UNIX_SOCKETS => Yes
PSL => No
HTTPS_PROXY => Yes
MULTI_SSL => Yes
BROTLI => Yes
Protocols => dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtmp, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
Host => arm-apple-darwin20.5.0
SSL Version => (SecureTransport) OpenSSL/1.1.1k
ZLib Version => 1.2.11
libSSH Version => libssh2/1.9.0

Directive => Local Value => Master Value
curl.cainfo => no value => no value
~/.composer/vendor/laravel/valet/bin/ngrok version
ngrok version 2.3.40
ls -al ~/.ngrok2
total 8
drwx------   3 ryan  staff    96 10 Mar 19:10 .
drwxr-xr-x+ 59 ryan  staff  1888 12 Aug 15:14 ..
-rw-------   1 ryan  staff    60 10 Mar 19:10 ngrok.yml
brew info nginx
nginx: stable 1.21.1 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/opt/homebrew/Cellar/nginx/1.21.1 (25 files, 2.2MB) *
  Poured from bottle on 2021-08-12 at 12:06:13
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@1.1, pcre
==> Options
--HEAD
    Install HEAD version
==> Caveats
Docroot is: /opt/homebrew/var/www

The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /opt/homebrew/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
  brew services start nginx
Or, if you don't want/need a background service you can just run:
  nginx
==> Analytics
install: 34,540 (30 days), 110,887 (90 days), 502,513 (365 days)
install-on-request: 34,473 (30 days), 110,628 (90 days), 499,078 (365 days)
build-error: 0 (30 days)
brew info php
php: stable 8.0.9 (bottled), HEAD
General-purpose scripting language
https://www.php.net/
/opt/homebrew/Cellar/php/8.0.9 (500 files, 78.0MB) *
  Poured from bottle on 2021-08-12 at 14:01:40
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/php.rb
License: PHP-3.01
==> Dependencies
Build: httpd, pkg-config
Required: apr, apr-util, argon2, aspell, autoconf, curl, freetds, gd, gettext, glib, gmp, icu4c, krb5, libffi, libpq, libsodium, libzip, oniguruma, openldap, openssl@1.1, pcre2, sqlite, tidy-html5, unixodbc
==> Options
--HEAD
    Install HEAD version
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php_module /opt/homebrew/opt/php/lib/httpd/modules/libphp.so

    
        SetHandler application/x-httpd-php
    

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /opt/homebrew/etc/php/8.0/

To start php:
  brew services start php
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/php/sbin/php-fpm --nodaemonize
==> Analytics
install: 52,148 (30 days), 148,440 (90 days), 625,675 (365 days)
install-on-request: 51,245 (30 days), 145,790 (90 days), 613,636 (365 days)
build-error: 0 (30 days)
brew info openssl
openssl@1.1: stable 1.1.1k (bottled) [keg-only]
Cryptography and SSL/TLS Toolkit
https://openssl.org/
/opt/homebrew/Cellar/openssl@1.1/1.1.1k (8,071 files, 18MB)
  Poured from bottle on 2021-04-15 at 12:37:45
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openssl@1.1.rb
License: OpenSSL
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /opt/homebrew/etc/openssl@1.1/certs

and run
  /opt/homebrew/opt/openssl@1.1/bin/c_rehash

openssl@1.1 is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS provides LibreSSL.

If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

For compilers to find openssl@1.1 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"

==> Analytics
install: 546,389 (30 days), 1,741,494 (90 days), 8,687,108 (365 days)
install-on-request: 38,071 (30 days), 124,143 (90 days), 1,024,009 (365 days)
build-error: 0 (30 days)
openssl version -a
LibreSSL 2.8.3
built on: date not available
platform: information not available
options:  bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: information not available
OPENSSLDIR: "/private/etc/ssl"
openssl ciphers
ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:GOST2012256-GOST89-GOST89:DHE-RSA-CAMELLIA256-SHA256:DHE-RSA-CAMELLIA256-SHA:GOST2001-GOST89-GOST89:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:CAMELLIA256-SHA256:CAMELLIA256-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:CAMELLIA128-SHA256:CAMELLIA128-SHA:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:RC4-SHA:RC4-MD5:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:DES-CBC3-SHA
sudo nginx -t
nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
which -a php-fpm
/opt/homebrew/opt/php/sbin/php-fpm
/opt/homebrew/sbin/php-fpm
/usr/sbin/php-fpm
/opt/homebrew/opt/php/sbin/php-fpm -v
PHP 8.0.9 (fpm-fcgi) (built: Jul 29 2021 08:52:25)
Copyright (c) The PHP Group
Zend Engine v4.0.9, Copyright (c) Zend Technologies
    with Xdebug v3.0.4, Copyright (c) 2002-2021, by Derick Rethans
    with Zend OPcache v8.0.9, Copyright (c), by Zend Technologies
sudo /opt/homebrew/opt/php/sbin/php-fpm -y /opt/homebrew/etc/php/8.0/php-fpm.conf --test
[12-Aug-2021 15:46:17] NOTICE: configuration file /opt/homebrew/etc/php/8.0/php-fpm.conf test is successful
ls -al ~/Library/LaunchAgents | grep homebrew
-rw-r--r--   1 ryan  staff   590  9 Mar 18:47 homebrew.mxcl.mailhog.plist
ls -al /Library/LaunchAgents | grep homebrew

ls -al /Library/LaunchDaemons | grep homebrew
-rw-r--r--   1 root  admin   602 12 Aug 15:43 homebrew.mxcl.dnsmasq.plist
-rw-r--r--   1 root  admin   577 12 Aug 15:44 homebrew.mxcl.nginx.plist
-rw-r--r--   1 root  admin   586 12 Aug 15:43 homebrew.mxcl.php.plist
ls -al /Library/LaunchDaemons | grep "com.laravel.valet."

ls -aln /etc/resolv.conf
lrwxr-xr-x  1 0  0  22  1 Jan  2020 /etc/resolv.conf -> ../var/run/resolv.conf
cat /etc/resolv.conf
#
# macOS Notice
#
# This file is not consulted for DNS hostname resolution, address
# resolution, or the DNS query routing mechanism used by most
# processes on this system.
#
# To view the DNS configuration used by this system, use:
#   scutil --dns
#
# SEE ALSO
#   dns-sd(1), scutil(8)
#
# This file is automatically generated.
#
search lan
nameserver 10.255.88.1
ifconfig lo0
lo0: flags=8049 mtu 16384
    options=1203
    inet 127.0.0.1 netmask 0xff000000 
    inet6 ::1 prefixlen 128 
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
    nd6 options=201
sh -c 'echo "------\n/opt/homebrew/etc/nginx/valet/valet.conf\n---\n"; cat /opt/homebrew/etc/nginx/valet/valet.conf | grep -n "# valet loopback"; echo "\n------\n"'
------
/opt/homebrew/etc/nginx/valet/valet.conf
---

3:    #listen VALET_LOOPBACK:80; # valet loopback

------
sh -c 'for file in ~/.config/valet/dnsmasq.d/*; do echo "------\n~/.config/valet/dnsmasq.d/$(basename $file)\n---\n"; cat $file; echo "\n------\n"; done'
------
~/.config/valet/dnsmasq.d/tld-test.conf
---

address=/.test/127.0.0.1
listen-address=127.0.0.1

------
sh -c 'for file in ~/.config/valet/nginx/*; do echo "------\n~/.config/valet/nginx/$(basename $file)\n---\n"; cat $file | grep -n "# valet loopback"; echo "\n------\n"; done'
------
~/.config/valet/nginx/claims.test
---

3:    #listen 127.0.0.1:80; # valet loopback
10:    #listen 127.0.0.1:443 ssl http2; # valet loopback
54:    #listen 127.0.0.1:60; # valet loopback

------
drbyte commented 3 years ago

"502 Bad Gateway" typically means that the "gateway process making the request" couldn't complete. Often this is php-fpm which is failing to respond, or is encountering an error. Usually the cause of the failure is in the php-fpm logs, or sometimes in the nginx logs. Sometimes the cause is a dns lookup failure ... but if normal web-surfing is fine, then if the dns lookup is failing for one particular destination it's likely that you've got a local dns config override issue such as something you added to /etc/hosts

drbyte commented 3 years ago

You didn't mention it, but I suppose if your issue is related to Laravel DebugBar then this proposed change might benefit you: https://github.com/laravel/valet/pull/1079/files ... You'd have to apply it to your existing config and restart. If it resolves your situation, please post to that PR's discussion saying so. If it makes no difference then it's unrelated.

ryangjchandler commented 3 years ago

I have already tried the fastcgi params inside of the sites config unfortunately and had no luck.

The php-fpm log didn't show anything unusual (it was completely empty), the nginx.log file did contain the following line:

2021/08/12 21:52:45 [error] 280#0: *11 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: claims.test, request: "GET /search HTTP/2.0", upstream: "fastcgi://unix:/Users/ryan/.config/valet/valet.sock:", host: "claims.test"

I've tried restarting both Nginx and PHP via valet restart, completely uninstalled Valet and reinstalled with no luck.

ryangjchandler commented 3 years ago

Also the request isn't failing for one particular destination. Any request to an external site, e.g. https://jsonplaceholder.typicode.com/ will fail, as well as any request from one Valet site to another.

drbyte commented 3 years ago

Quick suggestion to try:

https://github.com/laravel/valet/issues/968#issuecomment-656454281

This adds a specific dns resolver to dnsmasq for non local sites. You can put whatever source you want.

ryangjchandler commented 3 years ago

Still no luck with one, or both, of those files inside of the dnsmasq.d folder.

ryangjchandler commented 3 years ago

@drbyte Still having issues with this, is there anything else I can try? I'm unable to use Valet with any site that uses Stripe, S3, anything.

driesvints commented 2 years ago

Closing this issue because it's inactive, already solved, old or not relevant anymore. Feel to open up a new issue if you're still experiencing this.

ryangjchandler commented 2 years ago

Thanks @driesvints. The problem was actually the libvips extension interfering with the requests. Disabling fixed it all.

johnvoncolln commented 2 years ago

@ryangjchandler did you ever figure out why vips is making this happen? Recently upgraded from php7.4 to 8.0 and this happened, but disabling the extension cures the issue...

boris-glumpler commented 2 years ago

Just came across this issue. I still had ext-vips installed, even tho v2 of jcupitt/vips uses FFI now and doesn't actually need ext-vips anymore, so removing the extension fixes the issue.

nicmare commented 1 year ago

today i was trying to install/ using vips and also discovered it breaks my pages. no matter if i use php8.0. or 8.2.4. as soon as i enable vips extension and restart php service, all pages are broken. but i wonder if someone managed to get all running successfully? i mean vips should be faster than imagick. i am curious to give it try.