Configuring the Caddy Web Server

The API Platform distribution is shipped with the Caddy web server. The build contains the Mercure and the Vulcain Caddy modules.

API プラットフォーム ディストリビューションには、Caddy Web サーバーが同梱されています。ビルドには、Mercure および Vulcain Caddy モジュールが含まれています。

Caddy is positioned in front of the web API and of the Progressive Web App. It routes requests to either service depending on the value of the Accept HTTP header or the extension of the requested file.

Caddy は、Web API およびプログレッシブ Web アプリの前に配置されます。Accept HTTP ヘッダーの値または要求されたファイルの拡張子に応じて、要求をいずれかのサービスにルーティングします。

Using the same domain to serve the API and the PWA improves performance by preventing unnecessary CORS preflight requests and encourages embracing the REST principles.

同じドメインを使用して API と PWA を提供すると、不要な CORS プリフライト リクエストが防止されるためパフォーマンスが向上し、REST 原則の採用が促進されます。

By default, requests having an Accept request header containing the text/html media type are routed to the Next.js application, except for some paths known to be resources served by the API (e.g. the Swagger UI documentation, static files provided by bundles...). Other requests are routed to the API.

デフォルトでは、text/html メディア タイプを含む Accept リクエスト ヘッダーを持つリクエストは、Next.js アプリケーションにルーティングされます。ただし、API によって提供されるリソースであることが知られている一部のパス (Swagger UI ドキュメント、バンドルによって提供される静的ファイルなど) を除きます。 ..).その他のリクエストは API にルーティングされます。

Sometimes, you may want to let the PHP application generate HTML responses. For instance, when you create your own Symfony controllers serving HTML pages, or when using bundles such as EasyAdmin or SonataAdmin.

場合によっては、PHP アプリケーションに HTML 応答を生成させたい場合があります。たとえば、HTML ページを提供する独自の Symfony コントローラーを作成する場合や、EasyAdmin や SonataAdmin などのバンドルを使用する場合です。

To do so, you have to tweak the rules used to route the requests. Open api-platform/api/docker/caddy/Caddyfile and modify the expression. You can use any CEL (Common Expression Language) expression supported by Caddy.

そのためには、リクエストのルーティングに使用されるルールを微調整する必要があります。api-platform/api/docker/caddy/Caddy ファイルを開き、式を変更します。Caddy でサポートされている任意の CEL (Common Expression Language) 式を使用できます。

For instance, if you want to route all requests to a path starting with /admin to the API, modify the existing expression like this:

たとえば、/admin で始まるパスへのすべてのリクエストを API にルーティングする場合は、既存の式を次のように変更します。

# Matches requests for HTML documents, for static files and for Next.js files,
# except for known API paths and paths with extensions handled by API Platform
@pwa expression `(
        {header.Accept}.matches("\\btext/html\\b")
-        && !{path}.matches("(?i)(?:^/docs|^/graphql|^/bundles/|^/_profiler|^/_wdt|\\.(?:json|html$|csv$|ya?ml$|xml$))")
+        && !{path}.matches("(?i)(?:^/admin|^/docs|^/graphql|^/bundles/|^/_profiler|^/_wdt|\\.(?:json|html$|csv$|ya?ml$|xml$))")
    )`