Cant get caddy + FPM to work

1. The problem I’m having:

Hello,
i have a Symfony project and wanted to switch from nginx to caddy because of te easyness of the ssl certificates, but i can’t get it to work, i’m just getting 404. But with nginx, it is working. I think both configs are very similar and i don’t understand why it is not working with caddy.

2. Error messages and/or full log output:

3. Caddy version:

2.7

4. How I installed and ran Caddy:

Using Docker

a. Caddy:

docker-compose.yml

version: '3.3'
services:
  caddy:
    container_name: caddy
    image: caddy:2.7
    volumes:
      - './Caddyfile:/etc/caddy/Caddyfile'
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
      - "443:443/udp"
networks:
  default:
    name: proxynet
    external: true

Caddyfile

projectdomain {
        root * /app/public
        encode zstd gzip
        file_server
        php_fastcgi php-fpm:9000 {
                resolve_root_symlink
        }
        @phpFile {
                path *.php*
        }
        error @phpFile "Not found" 404
}

b. Nginx:

docker-compose.yml

version: '3.3'

services:
  nginx:
      image: nginx:1.25.2-alpine3.18
      container_name: nginx
      ports:
          - "8034:80"
      volumes:
          - ./nginx.conf:/etc/nginx/conf.d/default.conf

networks:
  default:
    name: proxynet
    external: true

nginx.conf

server {
    listen 80;
    server_name localhost;

    root /app/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $document_root;
        internal;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

Note: When i set a wrong fpm host in the Caddyfile, the result is the same.

I set it up using this guide.
Symfony Docs

You need to mount your code to /app in both the Caddy and php-fpm containers.

Why?
nginx is communicating through port 9000 to fpm and it’s working, so i don’t get why caddy should have access to /app while nginx has no access to /app…

I understand your point. I removed file server, now thr 404 is gone and i get an 200, but it is empty. And when i put in a wrong fpm host, it is also 200. So the fpm is not being contacted by caddy i think, but there are no logs.

Caddy’s try_files works slightly differently than Nginx’s. Caddy’s requires the file to exist on disk for the last entry (e.g. index.php) but nginx allows it to not exist on disk and rewrites to that anyway.

Caddy needs to see the PHP files to know how to rewrite to them, and for file_server to work to serve your static assets.

1 Like

Thank you very much. So there is no easy solution to this?

The easy solution is to mount your files to your Caddy container. That’s not difficult at all. That’s what everyone who uses Caddy + PHP does.