Search code examples
phpcaddy

Visiting an invalid url loads index.php


I am using the caddy web server to server my webpages. Caddy is configured to send the web pages through fpm-php.

The problem occurs when I access and invalid url, which is not on my server. For example example.com/invalid/url/which/does/not/exist.php. Instead of a 404 error page the index.php is returned, with a status code of 200.

I had managed to trace this to php, when disabling php in my caddyfile it works. But then I can't use php :->. Disabling rewrite does not help.

CaddyFile:

:80 {
    # Set this path to your site's directory.
    root * /var/www/example.com

    # Enable the static file server.
    file_server

    # Or serve a PHP site through php-fpm:
    php_fastcgi localhost:9000
    
    # Forward link without an extension to .php files
    try_files {path}.php {path}
    
    # Encode files to reduce size during transfer
    encode gzip
}

Expected: A 404 page is displayed, and/or a 404 status code is returned.

Thanks!


Solution

  • Thanks for your help! I found this in the caddyserver docs:

    It expects that any index.php at the site root acts as a router. If that is not desirable, either reconfigure the try_files option to modify the default rewrite behaviour, or take the expanded form below as a basis and customize it to your needs.
    

    Looks like I need to change the configuration a bit.

    Edit:

    Just had to change how caddy looks for files. Change the try_files on the php_fastcgi. Last example on the caddy docs.