如何配置 caddy 为特定 url 传送静态文件?

How to configure caddy to deliver static file for specific url?

我已经使用 API Platform distribution 创建了一个 api 平台项目。

它带有一个 caddy 服务器,配置如下:

{
    http_port {$CADDY_PORT}
    # Debug
    {$DEBUG}
    # HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
}

{$SERVER_NAME}

log

route {
    root * /srv/api/public
    mercure {
        # Transport to use (default to Bolt)
        transport_url {$MERCURE_TRANSPORT_URL:bolt:///data/mercure.db}
        # Publisher JWT key
        publisher_jwt {env.MERCURE_PUBLISHER_JWT_KEY} {env.MERCURE_PUBLISHER_JWT_ALG}
        # Subscriber JWT key
        subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY} {env.MERCURE_SUBSCRIBER_JWT_ALG}
        # Allow anonymous subscribers (double-check that it's what you want)
        anonymous
        # Enable the subscription API (double-check that it's what you want)
        subscriptions
        # Extra directives
        {$MERCURE_EXTRA_DIRECTIVES}
    }
    vulcain
    push

    # Add links to the API docs and to the Mercure Hub if not set explicitly (e.g. the PWA)
    header ?Link `</docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation", </.well-known/mercure>; rel="mercure"`
    # Disable Google FLOC tracking if not enabled explicitly: https://plausible.io/blog/google-floc
    header ?Permissions-Policy "interest-cohort=()"

    php_fastcgi unix//var/run/php/php-fpm.sock
    encode zstd gzip
    file_server
}

使用此配置,所有传入的 http 请求都由根目录 /srv/api/public.[=21 中我的 index.php(这是 file_server 指令的默认文件)文件处理=]

我想配置 caddy 以防止请求以 /file 开头时由 index.php 文件处理请求。这样,在我的 /srv/api/public 中,我可以创建一个文件夹 file 并直接请求我将放入的图像,如:http://localhost/file/my-image.png

我怎样才能更改我的 caddy 文件配置来做到这一点?

谢谢。

你只需要使用handle_path指令:

{$SERVER_NAME}

log

# add this directive
handle_path /file* {
    root * /srv/api/public/file
    file_server
}

route {
    root * /srv/api/public