Caddy Serving PHP Files

1. Caddy version (caddy version):

v2.4.0

2. How I run Caddy:

On a VPS server running caddy run --watch in a screen and using a Caddyfile for the config.

a. System environment:

CentOS 7

b. Command:

caddy run --watch

c. Service/unit/compose file:

I just run Caddy in a screen so I can watch for errors.

d. My complete Caddyfile or JSON config:

test1.domain.com, test2.domain.com {
        root * /home/customer-sites
        encode gzip
        try_files {path} index.php
        php_fastcgi 127.0.0.1:9000
        file_server
}

admin.domain.com {
        root * /home/admin
        encode gzip
        try_files {path} index.php
        php_fastcgi 127.0.0.1:9000
        file_server
}

3. The problem I’m having:

When I browse to the domains in my browser I get a “File not found.” error.

4. Error messages and/or full log output:

File not found.

5. What I already tried:

I’ve tried making php_fastcgi use my php-fpm.sock instead but I can’t find that anywhere.
As you can see from this screenshot php-fpm is running: https://i.hcloud.uk/k10FF9

6. Links to relevant resources:

N/A

Remove this. The php_fastcgi directive already has the appropriate try_files logic built in. See the expanded form to see how it works:

1 Like

Still getting “File not found.” :frowning:

Add this to the top of your Caddyfile, then check your logs to see what it’s doing:

{
	debug
}
1 Like

Thanks, where is the error log file path?

You should see it in stdout/stderr. You said you’re running it with caddy run so you should just see the logs right in your terminal.

Ah yes…

2021/05/15 20:10:29.698 DEBUG   http.handlers.rewrite   rewrote request {"request": {"remote_addr": "5.67.69.44:51372", "proto": "HTTP/2.0", "method": "GET", "host": "app-01.letrixlabs.com", "uri": "/", "headers": {"Accept-Encoding": ["gzip, deflate, br"], "Cache-Control": ["max-age=0"], "Upgrade-Insecure-Requests": ["1"], "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.9"], "Sec-Gpc": ["1"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["en-GB,en-US;q=0.9,en;q=0.8"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "app-01.letrixlabs.com"}}, "method": "GET", "uri": "/index.php"}
2021/05/15 20:10:29.699 DEBUG   http.reverse_proxy.transport.fastcgi    roundtrip       {"request": {"remote_addr": "5.67.69.44:51372", "proto": "HTTP/2.0", "method": "GET", "host": "app-01.letrixlabs.com", "uri": "/index.php", "headers": {"Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["en-GB,en-US;q=0.9,en;q=0.8"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"], "Upgrade-Insecure-Requests": ["1"], "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.9"], "Sec-Gpc": ["1"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "Accept-Encoding": ["gzip, deflate, br"], "X-Forwarded-For": ["5.67.69.44"], "Cache-Control": ["max-age=0"], "X-Forwarded-Proto": ["https"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "app-01.letrixlabs.com"}}, "dial": "127.0.0.1:9000", "env": {"AUTH_TYPE":"","CONTENT_LENGTH":"","CONTENT_TYPE":"","DOCUMENT_ROOT":"/home/admin","DOCUMENT_URI":"/index.php","GATEWAY_INTERFACE":"CGI/1.1","HTTPS":"on","HTTP_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.9","HTTP_ACCEPT_ENCODING":"gzip, deflate, br","HTTP_ACCEPT_LANGUAGE":"en-GB,en-US;q=0.9,en;q=0.8","HTTP_CACHE_CONTROL":"max-age=0","HTTP_HOST":"app-01.letrixlabs.com","HTTP_SEC_FETCH_DEST":"document","HTTP_SEC_FETCH_MODE":"navigate","HTTP_SEC_FETCH_SITE":"none","HTTP_SEC_FETCH_USER":"?1","HTTP_SEC_GPC":"1","HTTP_UPGRADE_INSECURE_REQUESTS":"1","HTTP_USER_AGENT":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36","HTTP_X_FORWARDED_FOR":"5.67.69.44","HTTP_X_FORWARDED_PROTO":"https","PATH_INFO":"","QUERY_STRING":"","REMOTE_ADDR":"5.67.69.44","REMOTE_HOST":"5.67.69.44","REMOTE_IDENT":"","REMOTE_PORT":"51372","REMOTE_USER":"","REQUEST_METHOD":"GET","REQUEST_SCHEME":"https","REQUEST_URI":"/","SCRIPT_FILENAME":"/home/admin/index.php","SCRIPT_NAME":"/index.php","SERVER_NAME":"app-01.letrixlabs.com","SERVER_PROTOCOL":"HTTP/2.0","SERVER_SOFTWARE":"Caddy/v2.4.0","SSL_CIPHER":"TLS_AES_128_GCM_SHA256","SSL_PROTOCOL":"TLSv1.3"}}
2021/05/15 20:10:29.699 DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": "127.0.0.1:9000", "request": {"remote_addr": "5.67.69.44:51372", "proto": "HTTP/2.0", "method": "GET", "host": "app-01.letrixlabs.com", "uri": "/index.php", "headers": {"Upgrade-Insecure-Requests": ["1"], "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.9"], "Sec-Gpc": ["1"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-Mode": ["navigate"], "Accept-Encoding": ["gzip, deflate, br"], "X-Forwarded-For": ["5.67.69.44"], "Cache-Control": ["max-age=0"], "X-Forwarded-Proto": ["https"], "Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Accept-Language": ["en-GB,en-US;q=0.9,en;q=0.8"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "proto_mutual": true, "server_name": "app-01.letrixlabs.com"}}, "headers": {"Status": ["404 Not Found"], "X-Powered-By": ["PHP/7.4.16"], "Content-Type": ["text/html; charset=UTF-8"]}, "status": 404}```

Alright, well the 404 is coming from your app and not Caddy. So you’ll need to figure out what’s not working in your app’s routing.

1 Like

Hmm, /home/admin has permissions for caddy:caddy, other than that I’m not really sure what else to look for?

Well PHP also needs permissions to those files. It depends what user php-fpm is running as.

php-fpm is running as caddy, unless this should be something else? It appears that html files work just fine but php files are getting the file not found message.

Well, the 404 is definitely served by PHP, so the problem is not Caddy at this point. You’ll need to dig into your application to understand how it’s handling the fastcgi request from Caddy.

Thanks, did a reinstall on the VPS as it was running apache prior, got it working within a few minutes.

Cheers for your time and help!

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