beyondcode / herd-community

82 stars 1 forks source link

[Bug]: Valet nginx config uses integers in place of the script paths, leading to infinite redirects #327

Closed lukasbestle closed 7 months ago

lukasbestle commented 7 months ago

Operating system version

macOS Ventura 13.6.3

System architecture

ARM64 (M1, M2, etc)

Herd Version

1.4.0 (Build 20)

PHP Version

No response

Bug description

A few weeks ago (still on 1.3.2), one of my sites using the built-in HTTPS stopped working. I got infinite redirects to an integer path like https://my-site.test/32. I didn't have the time to investigate, so I disabled HTTPS for now and the site started to work again.

Now (seemingly after the 1.4.0 update), all of my sites show this behavior and even if I disable HTTPS. As a first step I have uninstalled Herd according to the steps described in the docs. Afterwards I reinstalled Herd. The same thing happens, but the integer keeps counting up each time Herd/Valet adds a new site to its management (it is now at 55 for all sites).

I noticed that the 55 is in the nginx config files at ~/Library/Application Support/Herd/config/valet/Nginx/*, the rewrite rule likely being the source for the infinite redirect behavior. Here's an example:

# ISOLATED_PHP_VERSION=8.3
server {
    listen 127.0.0.1:80;
    server_name sandbox.test www.sandbox.test *.sandbox.test;
    #listen VALET_LOOPBACK:80; # valet loopback
    root /;
    charset utf-8;
    client_max_body_size 128M;

    location /<omitted, a UUID is placed here>/ {
        internal;
        alias /;
        try_files $uri $uri/;
    }

    location / {
        rewrite ^ "55" last;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    access_log off;
    error_log "/Users/lux/Library/Application Support/Herd/Log/nginx-error.log";

    error_page 404 "55";

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass $herd_sock_83;
        fastcgi_index "55";
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME "55";
        fastcgi_param HERD_HOME "/Users/lux/Library/Application Support/Herd";
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

I don't have any idea where to continue debugging this. Please help 🙏

Steps to reproduce

No response

Relevant log output

nginx log:

2024/02/06 21:54:19 [error] 12110#487092: *2 open() "54" failed (2: No such file or directory), client: 127.0.0.1, server: sandbox.test, reques$
2024/02/06 21:54:19 [error] 12110#487092: *2 open() "54" failed (2: No such file or directory), client: 127.0.0.1, server: sandbox.test, reques$
2024/02/06 21:54:19 [error] 12110#487092: *2 open() "54" failed (2: No such file or directory), client: 127.0.0.1, server: k-sandbox.test, reques$
2024/02/06 21:54:19 [error] 12110#487092: *2 open() "54" failed (2: No such file or directory), client: 127.0.0.1, server: k-sandbox.test, reques$
2024/02/06 21:54:19 [error] 12110#487092: *2 open() "54" failed (2: No such file or directory), client: 127.0.0.1, server: k-sandbox.test, reques$
2024/02/06 21:56:58 [error] 12941#491696: *1 js:
2024/02/06 21:56:58 [error] 12941#491696: *1 js: default
2024/02/06 21:56:58 [error] 12941#491696: *1 js:
2024/02/06 21:56:58 [error] 12941#491696: *1 js: default 
2024/02/06 21:57:29 [error] 12976#492557: *1 js:
2024/02/06 21:57:29 [error] 12976#492557: *1 js: default
2024/02/06 21:57:30 [error] 12976#492557: *1 js: 
2024/02/06 21:57:30 [error] 12976#492557: *1 js: default
2024/02/06 21:57:30 [error] 12976#492557: *1 js:
2024/02/06 21:57:30 [error] 12976#492557: *1 js: default
2024/02/06 21:57:30 [error] 12976#492557: *1 js:
2024/02/06 21:57:30 [error] 12976#492557: *1 js: default
2024/02/06 21:57:30 [error] 12976#492557: *1 js:
2024/02/06 21:57:30 [error] 12976#492557: *1 js: default
2024/02/06 22:01:44 [error] 13379#494598: *1 js:
2024/02/06 22:01:44 [error] 13379#494598: *1 js: default 
2024/02/06 22:02:16 [error] 13378#494597: *6 js:
2024/02/06 22:02:16 [error] 13378#494597: *6 js: default
2024/02/06 22:02:16 [error] 13378#494597: *6 js:
2024/02/06 22:02:16 [error] 13379#494598: *5 js: 
2024/02/06 22:02:16 [error] 13380#494599: *8 js:
2024/02/06 22:02:16 [error] 13378#494597: *6 js: default
2024/02/06 22:02:16 [error] 13379#494598: *5 js: default 
2024/02/06 22:02:16 [error] 13380#494599: *8 js: default
mpociot commented 7 months ago

When you isolate a site, the Herd CLI communicates with the Herd macOS application via an Apple Script. It seems like this does not work for you and I assume the 55, 54, etc. is the time that minute of the hour this command failed.

What do you get when you run this from your terminal:

osascript -e 'tell application "Herd"
        get valet path
end tell'

Just copy and paste it like this, including the newlines.

lukasbestle commented 7 months ago

Thank you very much for your super quick and helpful reply. It was spot-on.

The output I got was:

2024-02-07 20:34:56.689 osascript[2618:48473] ApplePersistence=NO
path:/Applications/Herd.app/Contents/Resources/valet/server.php

The reason for the first line was that I had manually disabled macOS auto-save globally via the defaults command. I now set this specifically for the apps where I need it, so the line is no longer printed by osascript. This immediately solved the issue for all sites after triggering a config rewrite in Herd (by switching the PHP version and then back again).

Posting this mainly to document the issue should it occur to anyone else in the future. But maybe you could still improve the robustness of the Herd setup by making the Herd CLI parse the osascript output in a way that ignores any additional output.

mpociot commented 7 months ago

Ohh interesting - I didn't know that here could be additional output! I'm glad that you got it working, and I'll try to improve the osascript output parsing!