Converting nginx's try_files to caddy

1. Output of caddy version:

v2.5.2 h1:eCJdLyEyAGzuQTa5Mh3gETnYWDClo1LjtQm2q9RNZrs=

2. How I run Caddy:

sudo systemctl start caddy

a. System environment:

OS: Ubuntu 22.04
Built with: caddy-dns/cloudflare (91cf700356a1cd0127bcc4e784dd50ed85794af5)

b. Command:

sudo caddy stop && sudo caddy start ./Caddyfile

or

sudo systemctl restart caddy

c. Service/unit/compose file:

[Unit]
Description=caddy
After=network.target

[Service]
WorkingDirectory=/home/user/caddy
Environment=
Type=simple
User=root
ExecStart=/usr/bin/caddy run /home/user/caddy/Caddyfile
Restart=on-failure

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

{
        acme_dns cloudflare <REDIACTED>
        log {
                output file /home/user/caddy/server.log
                format console
        }
}

onix.minjae.dev {
        php_fastcgi unix//run/php/php8.1-fpm.sock php {
                root /var/www/pterodactyl/public
                index index.php
                split ^(.+\.php)(/.+)$
                try_files {uri} /index.php?{uri}
        }
}

3. The problem I’m having:

I was moving the nginx configuration (full configuration) to Caddyfile, but I have trouble with try_files, how do I convert nginx’s

try_files $uri $uri/ /index.php?$query_string;

to caddy one?

(currently website only shows empty page)

4. Error messages and/or full log output:

N/A

5. What I already tried:

I’ve tried all things in the below links, but it seems they aren’t working.

6. Links to relevant resources:

Your config should just look like this:

onix.minjae.dev {
	root * /var/www/pterodactyl/public
	encode gzip
	php_fastcgi unix//run/php/php8.1-fpm.sock
	file_server
}

That’s all you need. Caddy’s php_fastcgi directive has built-in try_files, and it should work just fine out of the box in your case.

2 Likes

Thanks! It worked.

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