You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下Symfony集成Mercure报错“Failed to send an update”求助

排查Mercure "Failed to send an update" 错误的解决方案

以下是针对你配置的问题逐一排查和修复的步骤:

1. 修复Docker内部服务通信地址

PHP容器无法通过localhost访问宿主机的Mercure服务,在Docker Compose网络中,服务需通过服务名互相访问:

  • 修改.env中的MERCURE_URL:
    # 原配置(错误)
    # MERCURE_URL=http://localhost/.well-known/mercure
    # 修改为(正确)
    MERCURE_URL=http://mercure/.well-known/mercure
    
    原因:mercure是你在docker-compose.yml中定义的服务名,PHP容器可直接通过这个地址调用Mercure的发布接口。

2. 修正前端EventSource的协议与地址

JS脚本中存在协议不匹配和地址错误:

  • 你的Mercure容器仅配置了HTTP协议(80端口),需把JS中的https改为http:
    // 原代码(错误)
    // const eventSource = new EventSource("{{ mercure('https://localhost/books/1')|escape('js') }}");
    // 修改为(正确)
    const eventSource = new EventSource("{{ mercure('http://localhost/books/1')|escape('js') }}");
    
  • 确认MERCURE_PUBLIC_URL指向宿主机的Mercure地址:
    MERCURE_PUBLIC_URL=http://localhost/.well-known/mercure
    

可选:通过Symfony的81端口反向代理Mercure请求

如果希望前端统一通过81端口访问Mercure,需在Nginx配置中添加反向代理规则:
在./phpdocker/nginx/nginx.conf中添加:

location /.well-known/mercure {
    proxy_pass http://mercure/.well-known/mercure;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_cache_bypass $http_upgrade;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

此时更新MERCURE_PUBLIC_URL为:

MERCURE_PUBLIC_URL=http://localhost:81/.well-known/mercure

3. 检查端口占用冲突

Mercure容器映射了宿主机80端口,若该端口被其他服务占用,容器无法正常启动:

  • 执行docker ps查看Mercure容器状态,若未启动,查看日志:docker logs mercure
  • 若端口冲突,修改docker-compose.yml中的Mercure端口映射,例如:
    ports:
      - "8080:80"
      - "8443:443"
    
    同时更新.env中的对应地址:
    MERCURE_URL=http://mercure/.well-known/mercure
    MERCURE_PUBLIC_URL=http://localhost:8080/.well-known/mercure
    

4. 确认JWT密钥完全一致

确保.env中的密钥与Mercure容器的密钥完全匹配,建议去掉多余引号避免解析问题:

  • .env中:
    MERCURE_JWT_SECRET=!ChangeThisMercureHubJWTSecretKey!
    
  • docker-compose.yml中:
    MERCURE_PUBLISHER_JWT_KEY=!ChangeThisMercureHubJWTSecretKey!
    MERCURE_SUBSCRIBER_JWT_KEY=!ChangeThisMercureHubJWTSecretKey!
    

5. 验证Mercure服务可用性

  • 宿主机访问http://localhost/.well-known/mercure,若返回Mercure的Hub响应,说明服务正常;
  • 进入PHP容器测试内部连接:
    docker exec -it task-php-fpm bash
    curl http://mercure/.well-known/mercure
    
    若能正常响应,说明内部通信正常。

内容的提问来源于stack exchange,提问作者jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 13:55:02