How to serve static files with Caddy php_fastcgi for single page app?

86 Views Asked by At

I'm trying to convert my websites to a Docker setup using Caddy and fpm. I have one Laravel (v9) app that is uses a single .php file as entry point (/server.php) which handles routing. When running the app, all html is loaded correctly for my pages. The problem is that static files (.css, .js, .png, etc...) are being handled by the php_fastcgi and return a http status 200 with empty contents.

Caddyfile

(defaults_foo) {
    root * /apps/foo
    php_fastcgi foo:9000 {
        root /var/www/html
        try_files {path} server.php = 404
    }

    # Serve static files
    file_server {
        root /apps/foo/public
    }

    handle_errors {
        respond "{err.status_code} {err.status_text}"
    }
}

foo.nl {
    import defaults_foo

    tls {
        on_demand
    }
}

foo.local {
    import defaults_foo

    tls internal {
        on_demand
    }
}

docker-compose.yml

version: '3.8'

services:
  php_foo:
    hostname: foo
    build: /docker/php/.
    image: php_foo:fpm-alpine
    container_name: php_foo
    restart: unless-stopped
    volumes:
      - ./:/var/www/html
    networks:
      - caddy_net
    ports:
      - "${HTTP_PORT}:9000"

networks:
  caddy_net:
    external: true

Dockerfile

FROM php:8.3-fpm-alpine

RUN docker-php-ext-install mysqli pdo pdo_mysql

RUN apk add mc

I tried toying around with the try_files matching and file_server based on some other articles. However, I couldn't find any articles with the exact same issue.

One of the thing I tried, was adding the file_server to:

php_fastcgi foo:9000 {
        root /var/www/html
        try_files {path} server.php = 404
        files_server
    }

Which is not an available option

Error: adapting config using caddyfile: parsing caddyfile tokens for 'php_fastcgi': unrecognized subdirective files_server, at /apps/foo/docker/caddy/Caddyfile:6 import chain ['/etc/caddy/Caddyfile:6 (import)','/apps/foo/docker/caddy/Caddyfile:20 (import defaults_foo)']

I also tried adding a route for static files:

    route /static/* {
        file_server {
            root /apps/foo/public
        }
    }

I'm kinda lost to what the correct solution should be. Any help would be appreciated.

0

There are 0 best solutions below