Skip to content

Conversation

@dunglas
Copy link
Owner

@dunglas dunglas commented Nov 16, 2024

Expanded, the current config is similar to this:

# 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 index.php
	split_path .php
}
rewrite @indexFiles {http.matchers.file.relative}
# FrankenPHP!
@phpFiles path *.php
php @phpFiles
file_server

I recently took some time to profile and optimize FrankenPHP, and that config is the slowest part in FrankenPHP (this likely affects the php_fastcgi Caddy directive as well). All the calls to try_files and file do I/O operations, which are slow.
Most of these I/O operations are checks useful for traditional PHP apps where the document root contains a lot of .php files (like WordPress) but are not needed when using Symfony or Laravel.

This simplified config reduces the number of I/O to the strict minimum: just 1 check to see if the requested path exists on disk, and delegates to PHP if it's not the case.

In the future, I plan to add a framework subdirective to php_server that automatically generates this config.

cc @francislavoie, this may also be interesting for php_fastcgi.

@maxhelias maxhelias merged commit 1f3ea50 into main Nov 16, 2024
2 checks passed
@maxhelias maxhelias deleted the perf/improve-frankenphp-config branch November 16, 2024 13:17
@maxhelias
Copy link
Collaborator

Thanks @dunglas !
The latest FrankenPHP optimizations are a remarkable and impressive piece of work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants