Handle_errors on php_fastcgi returning index.php

1. Caddy version (caddy version):

v2.4.3 h1:Y1FaV2N4WO3rBqxSYA8UZsZTQdN+PwcoOcAiZTM8C0I=

2. How I run Caddy:

a. System environment:

Ubuntu 18.04.5 LTS with systemd service

b. Command:

n/a

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

dev.livefrom.space, livefrom.space {
        root * /var/www/dev.livefrom.space
        php_fastcgi unix//run/php/php8.0-fpm.sock
        file_server

        handle_errors {
                respond "{http.error.status_code} {http.error.status_text}"
        }
}

3. The problem I’m having:

I get the index file as a response every time even for urls that don’t exist.

ex: https://livefrom.space/ and https://livefrom.space/page-that-does-not-exist both return the index.php file in root directory.

4. Error messages and/or full log output:

n/a

5. What I already tried:

I have tried to set root matching pattern to / but then any css/ or js/ paths stop working.

6. Links to relevant resources:

https://livefrom.space

This looks like pretty much the same question as this one earlier today:

Thanks, I didn’t see that thread when looking for a solution.

The config I have now does what I wanted:

dev.livefrom.space, livefrom.space {
        root * /var/www/dev.livefrom.space
        file_server

        route {
                # Add trailing slash for directory requests
                @canonicalPath {
                        file {path}/index.php
                        not path */
                }
                redir @canonicalPath {path}/ 308

                # If the requested file does not exist, try index files
                @indexFiles file {
                        try_files {path} {path}/index.php 
                        split_path .php
                }
                rewrite @indexFiles {http.matchers.file.relative}

                # Proxy PHP files to the FastCGI responder
                @phpFiles path *.php
                reverse_proxy @phpFiles unix//run/php/php8.0-fpm.sock {
                        transport fastcgi {
                                split .php
                        }
                }
        }

        handle_errors {
                respond "{http.error.status_code} {http.error.status_text}" {http.error.status_code} 
        }
}

You can probably just use this:

php_fastcgi unix//run/php/php8.0-fpm.sock {
	index off
}

doing this resulted in the root path not returning the index file, you had to type out index.php in the url

Ah, in that case you can add this to cover it:

rewrite / /index.php

This will ensure only requests to the root will be rewritten by using a / matcher.

This topic was automatically closed after 30 days. New replies are no longer allowed.