I have read through every config example from every thread that I could find. The above example looks like it should be correct and it makes the most sense to me.
If you go to https://thefuture.weavers.space/not-there, I would expect that Caddy would trigger a 404 and display the 404 page: https://thefuture.weavers.space/not-found. Instead, the homepage is displayed.
Is the 404 coming from file_server, or from your PHP app?
If itβs from your PHP app, handle_errors does not get invoked because itβs an actual response from your app, and not an error in Caddy. The handle_errors routes only get invoked by errors triggered within Caddy.
Itβs not reaching file_server. Itβs being rewritten to /index.php by php_fastcgi, then being sent to php-fpm. At that point, itβs the responsibility of your PHP app to render the request.
The docs say that handle_errors should be able to catch errors from try_files. I could not make that happen.
I have my php_fastcgi config inside of an import so that I can use that same PHP configuration on multiple websites. However, different websites have different 404 pages. Is there a way that I can somehow dynamically set that error page in the try_files? Something like thisβ¦
Only if none of the rules prior to =404 donβt reach a file on disk. If /not-found/index.php actually exists, then it will rewrite to that, and never trigger a 404.
If you go to a page that triggers a 404, the PHP of the configured 404 page is downloaded instead of displayed. So then I tried forcing the Content Type to be HTML.
However, I am curious if rewriting a URL for a 404 is better than a redirect. Any thoughts on that? I have done some google searches and it seems that everyone does redirects. However, my gut feeling is that a rewrite would be better.
php_fastcgi will only send PHP scripts to php-fpm if the current path (rewritten to) has a .php file extension.
Please be more specific about which requests youβre making (the paths used). Are you expecting requests to /foo to run /foo.php? If so then you need try_files to try {path}.php as well, for example.
You donβt have a php_fastcgi in this block, so the only thing that runs is file_server. If you wanted to use PHP for your error page, then you need php_fastcgi in there as well.
When an error happens, it quits out of the current middleware pipeline, and starts running the error routes. That means the main routes and error routes are mutually exclusive, they donβt affect eachother. So you need to put everything necessary to handle the request in handle_errors as well.
I am still learning Caddyfile and what settings are allowed to go into what. Thank you very much for you help! Hopefully this thread will be helpful to others.