1

我有一个基于 symfony mercure.rocks 的聊天应用程序,我正在尝试从旧的遗留 mercure.rocks 配置切换到新的 caddy 服务器集线器配置。我厌倦了几种使它工作的方法,但由于某种原因我仍然无法使其工作,让我在下面分享配置细节。

旧的遗留 mercure.rocks 配置。

Systemd 自动启动配置

[Unit]
Description=Mercure service

[Install]
WantedBy=multi-user.target

[Service]
Environment="WRITE_TIMEOUT=3600s" "JWT_KEY=!ChangeMe!" "CERT_FILE=/certificatefilelocation/fullchain.pem" "KEY_FILE=/certificatefilelocation/privkey.pem"

ExecStart=/www/wwwroot/stage/tamilchat/mercure --addr=:3000 --subscriptions --debug --allow-anonymous --cors-allowed-origins='https://stage.tamilchatz.com' --publish-allowed-origins='*' 
Restart=always
RestartSec=5

这是我对 symfony 应用程序的配置

MERCURE_PUBLISH_URL=https://caddy.tamilchatz.com/mercure
MERCURE_JWT_KEY=notherealkey
MERCURE_ALLOW_ANONYMOUS=1
MERCURE_LISTENED_URI=https://caddy.tamilchatz.com/mercure
MERCURE_SUBSCRIPTIONS_URI=https://caddy.tamilchatz.com/mercure/subscriptions/    MERCURE_JWT_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
MERCURE_CORS_ALLOWED_ORIGINS=caddy.tamilchatz.com
MERCURE_PUBLISH_ALLOWED_ORIGINS=caddy.tamilchatz.com
MERCURE_ACCESS_CONTROL_ALLOW_ORIGIN=http://127.0.0.1
ACCESS_CONTROL_ALLOW_ORIGIN=http://127.0.0.1
SUBSCRIPTIONS_URI=https://caddy.tamilchatz.com/mercure
MERCURE_SUBSCRIPTIONS=1

这是当前的 caddy 服务器配置文件。

{
    # Debug mode (disable it in production!)
    {$DEBUG:debug}
    # HTTP/3 support
    servers {
        protocol {
            experimental_http3
        }
    }
}

{$SERVER_NAME:caddy.tamilchatz.com}
log

route {
    redir / /.well-known/mercure/ui/
    encode zstd gzip

    mercure {
        # Transport to use (default to Bolt)
        # Publisher JWT key
#transport_url {$MERCURE_TRANSPORT_URL:bolt://mercure.db}
jwt_key okthischanged
        publisher_jwt okthischanged
        # Subscriber JWT key
        subscriber_jwt okthischanged
        # Permissive configuration for the development environment
        cors_origins https://stage.tamilchatz.com
        publish_origins *
 #       demo
        anonymous
        subscriptions
        # Extra directives
        {$MERCURE_EXTRA_DIRECTIVES}
    }

这是分阶段服务器聊天链接 > https://stage.tamilchatz.com/chat

控制台中的错误:

Access to resource at 'https://caddy.tamilchatz.com/mercure?topic=One' from origin 'https://stage.tamilchatz.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
caddy.tamilchatz.com/mercure?topic=One:1 GET https://caddy.tamilchatz.com/mercure?topic=One net::ERR_FAILED
chat:1 Access to resource at 'https://caddy.tamilchatz.com/mercure?topic=events' from origin 'https://stage.tamilchatz.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
caddy.tamilchatz.com/mercure?topic=events:1 GET https://caddy.tamilchatz.com/mercure?topic=events net::ERR_FAILED

需要帮助才能解决这个问题。

4

0 回答 0