File server does not process `index` properly

1. The problem I’m having:

While accessing a directory, I get 404, even though there is an index.htm file. I wouldn’t want to configure each directory where there are no PHP files separately. It’s 25 years of stuff.

2. Error messages and/or full log output:

{
  "level": "info",
  "ts": 1743326483.5158207,
  "logger": "http.log.access.log6",
  "msg": "handled request",
  "request": {
    "remote_ip": "80.89.72.179",
    "remote_port": "60163",
    "client_ip": "80.89.72.179",
    "proto": "HTTP/3.0",
    "method": "GET",
    "host": "laacz.lv",
    "uri": "/f/misc/laacz-lv-versions/etzp/",
    "headers": {
      "Sec-Ch-Ua": [
        "\"Chromium\";v=\"134\", \"Not:A-Brand\";v=\"24\", \"Google Chrome\";v=\"134\""
      ],
      "Sec-Fetch-Mode": ["navigate"],
      "Sec-Ch-Ua-Mobile": ["?0"],
      "Accept-Language": ["en,lv;q=0.9,en-GB;q=0.8"],
      "Upgrade-Insecure-Requests": ["1"],
      "User-Agent": [
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
      ],
      "Referer": ["https://laacz.lv/about"],
      "Accept": [
        "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
      ],
      "Sec-Fetch-User": ["?1"],
      "Sec-Ch-Ua-Platform": ["\"macOS\""],
      "Priority": ["u=0, i"],
      "Sec-Fetch-Site": ["same-origin"],
      "Accept-Encoding": ["gzip, deflate, br, zstd"],
      "Sec-Fetch-Dest": ["document"],
      "Cache-Control": ["no-cache"],
      "Cookie": ["REDACTED"],
      "Pragma": ["no-cache"]
    },
    "tls": {
      "resumed": false,
      "version": 772,
      "cipher_suite": 4865,
      "proto": "h3",
      "server_name": "laacz.lv"
    }
  },
  "bytes_read": 0,
  "user_id": "",
  "duration": 0.038963097,
  "size": 2226,
  "status": 404,
  "resp_headers": {
    "Server": ["Caddy"],
    "Status": ["404 Not Found"],
    "Cache-Control": ["no-cache, private"],
    "Date": ["Sun, 30 Mar 2025 09:21:23 GMT"],
    "Content-Type": ["text/html; charset=UTF-8"],
    "Content-Encoding": ["zstd"],
    "Vary": ["Accept-Encoding"]
  }
}

3. Caddy version: 2.9.1

4. How I installed and ran Caddy: sudo apt install caddy

a. System environment: Debian 12.10

d. My complete Caddy config:

(logging) {
        log {
                output file /var/log/caddy/{args[0]}.access.log {
                        roll_size 20mb
                }
        }
}

(common) {
        encode zstd gzip
        php_fastcgi unix//var/run/php/php8.4-fpm.sock {
                resolve_root_symlink
        }
        file_server {
                hide .git .htaccess *.inc .idea *.phps *.bak
                index index.html index.htm index.txt index.php
        }
}

laacz.lv {
        root * /var/www/laacz.lv/public
        import common
        import logging laacz.lv
}

Please enable debug logs, make a request, and share the full set of logs for that request. It’ll show which paths Caddy looks for and why it returned 404.

Thanks. Took another look at the debug and noticed that request gets passed to /index.php. After that read through pthe expanded form](php_fastcgi (Caddyfile directive) — Caddy Documentation) of the php_fastcgi directive and noticed that it rewrites try_files with try_files {path} {path}/index.php /index.php, for which the last rule matches always if you’ve got an index.php file at the root of your website.

So, if one wants to match directory indexes as well, the correct order of try_files when using php_fastcgi would be:

try_files {path} {path}/index.php {path}/index.htm {path}/index.html /index.php

It’s all good now.

3 Likes

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