
mercure 是一个开放的网络协议,用于推送实时更新到 web 浏览器和其他客户端。通过与 symfony 框架集成,开发者可以轻松地为应用程序添加实时功能,如聊天、通知或仪表盘更新。本指南将详细介绍如何在 symfony 5 项目中配置和运行 mercure 服务,并解决常见的访问问题。
在开始之前,请确保您已具备以下环境:
Mercure Hub 是负责接收、存储和分发更新的核心服务。
在 Symfony 项目中,我们需要安装 Mercure Bundle 来方便地发布更新。
composer require symfony/mercure
此命令会安装必要的依赖并自动配置 Bundle。
Mercure Bundle 需要通过环境变量来知道 Mercure Hub 的地址和用于签名的密钥。
# See https://symfony.com/doc/current/mercure.html#configuration # The URL of the Mercure hub, used by the app to publish updates (can be a local URL) MERCURE_URL=https://127.0.0.1:8000/.well-known/mercure # The public URL of the Mercure hub, used by the browser to connect MERCURE_PUBLIC_URL=https://127.0.0.1:8000/.well-known/mercure # The secret used to sign the JWTs MERCURE_JWT_SECRET="!ChangeMe!" ###< symfony/mercure-bundle ###
重要提示: MERCURE_JWT_SECRET 在生产环境中必须是一个强随机密钥,并且不能是默认的 !ChangeMe!。
Mercure Hub 使用 Caddy 作为其 HTTP 服务器,它能够自动处理 TLS 证书。
启动 Mercure Hub: 打开 PowerShell 或命令提示符,导航到您解压 Mercure Hub 的目录(例如 C:mercure-hub)。然后执行以下命令来启动 Mercure 服务器:
$env:MERCURE_PUBLISHER_JWT_KEY='!ChangeMe!'; $env:MERCURE_SUBSCRIBER_JWT_KEY='!ChangeMe!'; .mercure.exe run -config Caddyfile.dev
命令解释:
如果一切正常,您会看到类似以下的日志输出,表明 Caddy 服务器已成功启动并监听 HTTPS 端口:
2021/11/16 01:39:58.029 INFO http server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2021/11/16 01:39:58.029 INFO http enabling automatic HTTP->HTTPS redirects {"server_name": "srv0"}
2021/11/16 01:39:58.111 INFO tls cleaning storage unit {"description": "FileStorage:C:\Users\toufi\AppData\Roaming\Caddy"}
2021/11/16 01:39:58.113 INFO tls finished cleaning storage units
2021/11/16 01:39:58.134 INFO pki.ca.local root certificate is already trusted by system {"path": "storage:pki/authorities/local/root.crt"}
2021/11/16 01:39:58.135 INFO http enabling automatic TLS certificate management {"domains": ["localhost"]}
2021/11/16 01:39:58.136 WARN tls stapling OCSP {"error": "no OCSP stapling for [localhost]: no OCSP server specified in certificate"}
2021/11/16 01:39:58.143 INFO autosaved config (load with --resume flag) {"file": "C:\Users\toufi\AppData\Roaming\Caddy\autosave.json"}
2021/11/16 01:39:58.143 INFO serving initial configuration这些日志表明 Mercure Hub 正在运行,并且 Caddy 已经为 localhost 启用了 HTTPS。
启动 Mercure Hub 后,您需要验证它是否可访问。
在浏览器中访问 Mercure 端点: 许多用户在尝试访问 Mercure 端点时可能会遇到“Not Found”错误,因为他们可能忘记指定端口。例如,直接访问 https://localhost/.well-known/mercure 可能会导致 Apache 或其他 Web 服务器响应,而不是 Mercure Hub。
正确的访问方式是包含端口号 8000:
https://localhost:8000/.well-known/mercure
当您在浏览器中访问 https://localhost:8000/.well-known/mercure 时,如果 Mercure Hub 运行正常,您应该会看到一个空白页面或一个简单的文本响应,表明连接成功,而不是“Not Found”错误。这是因为 Mercure Hub 的 Caddyfile.dev 配置使其在端口 8000 上监听请求,而不是默认的 HTTPS 端口 443。
通过遵循本教程的步骤,您应该能够成功地在 Symfony 5 项目中配置和运行 Mercure 服务,并开始构建您的实时应用程序。
以上就是Symfony 5 与 Mercure 集成:配置与常见问题解决指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号