Caddyfile for Mercure

1. The problem I’m having:

I’m trying to add Mercure to my existing Caddyfile (FrankenPHP).

This is my existing Caddyfile:

{
        debug
        log {
                output file /home/tnio_gvr5s/caddy.log {
                        roll_size 10mb
                        roll_keep 5
                        roll_keep_for 720h
                }
                level INFO
        }
}

easy-poly.fr {
        root * /var/mon-vps/web/easy-poly.fr

        handle / {
                try_files {path} index.php
                route {
                        php
                }
        }

        handle /_php* {
                route {
                        php
                }
        }

        try_files {path}.html
        handle {
                file_server
        }

        try_files {path} {path}/ =404
        handle_errors {
                rewrite * /error.php?err_status_code={err.status_code}&err_status_text={err.status_text}&err_status_message={err.status_message}&err_status_trace={err.status_trace}&err_status_id={err.status_id}
                php_server
        }

        log {
                level INFO
                format console
                output file /home/tnio_gvr5s/wwwlogs/caddy-easy-poly.fr.log {
                        roll_size 10MiB
                        roll_local_time
                        roll_keep 10
                        roll_keep_for 336h
                }
        }
}
www.easy-poly.fr {
        redir https://easy-poly.fr{uri} permanent
}

My goal was to make an effective Caddyfile:

  • 1 route for php files
  • another route for static files.
    ==> Am I right ?

And this is a tree view of my folders:

- php
    - templates
- assets
    - css
    - js
    - img
- fr (html)
- ge (html)
- en (html)

My existing Caddyfile works well :

  • php, html files and assets are well displayed.
  • error files are well catched.

But, when I try to add Mercure, it doesn’t work as I expected.
This is the new Caddyfile :

{
        debug
        log {
                output file /home/tnio_gvr5s/caddy.log {
                        roll_size 10mb
                        roll_keep 5
                        roll_keep_for 720h
                }
                level INFO
        }
}

easy-poly.fr {
        mercure {
                publisher_jwt D_C8nN@NYF-ifslz@!1CWdY+joY_b05+AADSm*iQ
                subscriber_jwt hwHG3aYD9SkLypa9a+kt6dD-EojLW-xN0BrCjv*O
                transport bolt {
                        path /home/tnio_ssh__7zys4mu4v/mercuretnio.db
                        bucket_name buck_tnio
                        size 1000
                        cleanup_frequency 0.5
                }
                cors_origins *
                publish_origins *
                subscriptions
                publish
                ui
        }

        root * /var/mon-vps/web/easy-poly.fr

        handle / {
                try_files {path} index.php
                route {
                        php
                }
        }

        handle /_php* {
                route {
                        php
                }
        }

        try_files {path}.html
        handle {
                file_server
        }

        try_files {path} {path}/ =404
        handle_errors {
                rewrite * /error.php?err_status_code={err.status_code}&err_status_text={err.status_text}&err_status_message={err.status_message}&err_status_trace={err.status_trace}&err_status_id={err.status_id}
                php_server
        }

        log {
                level INFO
                format console
                output file /home/tnio_gvr5s/wwwlogs/caddy-easy-poly.fr.log {
                        roll_size 10MiB
                        roll_local_time
                        roll_keep 10
                        roll_keep_for 336h
                }
        }
}
www.easy-poly.fr {
        redir https://easy-poly.fr{uri} permanent
}

Results:

  • php, html files and assets are well displayed.

  • error files are well catched.

  • But, after this curl command to publish :

curl -X POST https://easy-poly.fr/.well-known/mercure \\
-d 'topic=https://easy-poly.fr/books/1' \\
-d 'data={"foo": "updated value"}' \\
-H 'Authorization: Bearer epc2I6Ip7cW1DLmKi3x-iEdccgXf7-LVNzmKi3x-iEdccgXf7-LVNzmKi3x-iEdccgXf7-LVNzmKi3x-iEdccgXf7-LVNz2FkKhVbGUM'

==> I get redirected to error.php instead of getting uuid.

2. Error messages and/or full log output:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ERROR 404</title>
</head>

<body>
    <h1>ERROR !!!</h1>
<br>err_status_code 404 !!!
<br>err_status_text Not Found !!!
<br>err_status_message &lt;nil&gt; !!!
<br>err_status_trace &lt;nil&gt; !!!
<br>err_status_id &lt;nil&gt; !!!
<hr>page : error.php
<br>The current file name is: mercure
</body>

3. Caddy version:

frankenphp version:
v2.10.2

4. How I installed and ran Caddy:

Install PHP by compiling:

Compile the Go App using xcaddy:

a. System environment:

DEBIAN 13 (no Docker)

I’m not very familiar with mercure, so this is just a thought, something I’d try in your situation.

It looks like mercure doesn’t have a predefined directive order. You may need to set it explicitly with the order directive, or place it inside a route block to control execution. Without that, Caddy will just hit the handle / block and give your 404 Not Found.

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