Caddy: Caddy V2 `php_fastcgi` directive unsets `file_server` default index `index.html`

Created on 4 Aug 2020  ยท  6Comments  ยท  Source: caddyserver/caddy

Does php_fastcgi unset the file_server default index index.html?

Steps to reproduce:

.
โ”œโ”€โ”€ index.php
โ”œโ”€โ”€ docs
โ”‚ย ย  โ”œโ”€โ”€ index.html

With only file_server enabled, access /docs/ returns content from /docs/index.html. But if we add php_fastcgi directive along with the file_server, access /docs/ will be returning 404 not found instead.

php_fastcgi unix//run/php/php-version-fpm.sock
file_server

The solution I found is to add another try_files directive like:

php_fastcgi unix//run/php/php-version-fpm.sock
file_server
try_files {path}/index.html

Not sure it's a bug or intended feature? We know that in caddy v1 the index was both index.html and index.php.

Thank you in advance.

question

Most helpful comment

Yeah - it could be done, but allow for more than one index file is sorta tricky because it would need to expand the try_files out to both the {path}/{indexfile} and {indexfile} attempts basically. The try_files would effectively become 2n+1 lookups where n is the number of index files.

The thing is that php_fastcgi shouldn't really make assumptions about non-PHP usecases I think.

The try_files would effectively look like this:

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

Maybe we should only expand the {path}/{indexfile} part and not the {indexfile} part? This assumes that you only enable php_fastcgi with a root that has a PHP index file and not any other kind:

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

There's some pros and cons to weigh here and I'm not convinced by any of the options.

All 6 comments

Good question.

This is because php_fastcgi includes its own try_files logic (see the expanded form https://caddyserver.com/docs/caddyfile/directives/php_fastcgi).

I think if you specify index.html as an additional index file, it might do the trick.

php_fastcgi unix//run/php/php-version-fpm.sock {
    index index.php index.html
}

For next time, please ask your usage questions on the Caddy community forums. We prefer to keep the GitHub issue board for bugs and feature requests. If it's deemed to be likely a bug after discussion, we'll likely point you to open an issue here afterwards. Don't forget to fill out the thread template so we can help you!

Oh, sorry - I just checked the code, the index subdirective doesn't support more than one option currently. That's a bug with the documentation. I'll have that fixed.

Anyways, I believe your solution to be the correct approach.

Thanks. Forgot to mention I've also checked the code. index subdirective of php_fastcgi not as same as file_server's.
It'd be great if we can support it rather than fix the documentation. ๐Ÿ˜„

@chuangbo We'd happily welcome a pull request! (At least, I think so -- Francis is more knowledgable about the PHP stuff.)

Yeah - it could be done, but allow for more than one index file is sorta tricky because it would need to expand the try_files out to both the {path}/{indexfile} and {indexfile} attempts basically. The try_files would effectively become 2n+1 lookups where n is the number of index files.

The thing is that php_fastcgi shouldn't really make assumptions about non-PHP usecases I think.

The try_files would effectively look like this:

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

Maybe we should only expand the {path}/{indexfile} part and not the {indexfile} part? This assumes that you only enable php_fastcgi with a root that has a PHP index file and not any other kind:

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

There's some pros and cons to weigh here and I'm not convinced by any of the options.

Eeek, that's complicated. Let's wait and see how necessary that really is first.

Was this page helpful?
0 / 5 - 0 ratings