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

Docker+Nginx部署Mercure遭遇404错误求助

Symfony 7 + Mercure Docker部署404问题解决指南

问题核心

你遇到的Nginx 404错误,根源在于Mercure hub的默认路由与你的配置不匹配:Mercure默认在/.well-known/mercure提供服务,而非你配置的/mercure/.well-known,同时Nginx转发规则、.env中的URL配置均存在路径错误。另外,Nginx运行在宿主机,无法直接解析Docker容器名称,必须使用宿主机映射的端口访问Mercure。


解决方案步骤

1. 调整Docker Compose配置(二选一)

方案A:使用Mercure默认路径(推荐)

无需修改现有Docker Compose配置,Mercure默认会在/.well-known/mercure提供服务。

方案B:自定义/mercure前缀访问

若希望通过/mercure前缀访问Mercure,需修改Docker Compose中Mercure的环境变量,添加路径路由规则:

mercure:
  image: dunglas/mercure
  restart: unless-stopped
  container_name: flex_mercure
  ports:
    - "3000:3000"
  environment:
    SERVER_NAME: ':3000'
    MERCURE_PUBLISHER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
    MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
    MERCURE_PUBLISH_URL: '/mercure'
    MERCURE_EXTRA_DIRECTIVES: |
      cors_origins https://flex-test.xyz
      use_forwarded_headers "1"
      route /mercure/* {
        uri strip_prefix /mercure
        reverse_proxy http://localhost:3000/.well-known/mercure
      }
  volumes:
    - mercure_data:/data
    - mercure_config:/config

2. 修正.env配置

根据选择的方案调整Mercure URL:

方案A(默认路径)

###> symfony/mercure-bundle ###
MERCURE_URL=http://127.0.0.1:3000/.well-known/mercure
MERCURE_PUBLIC_URL=https://flex-test.xyz/.well-known/mercure
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
###< symfony/mercure-bundle ###

方案B(自定义前缀)

###> symfony/mercure-bundle ###
MERCURE_URL=http://127.0.0.1:3000/mercure
MERCURE_PUBLIC_URL=https://flex-test.xyz/mercure
MERCURE_JWT_SECRET="!ChangeThisMercureHubJWTSecretKey!"
###< symfony/mercure-bundle ###

3. 修正Nginx转发规则

替换原有的location /mercure/.well-known块:

方案A(默认路径)

location /.well-known/mercure {
    proxy_pass http://127.0.0.1:3000/.well-known/mercure;
    proxy_read_timeout 24h;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
}

方案B(自定义前缀)

location /mercure {
    proxy_pass http://127.0.0.1:3000/mercure;
    proxy_read_timeout 24h;
    proxy_http_version 1.1;
    proxy_set_header Connection "";

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
}

4. 重启服务

  • 重启Mercure容器:docker-compose restart mercure
  • 重启Nginx:sudo systemctl restart nginx

额外说明

  • 为什么不能用Docker容器名访问?因为Nginx运行在宿主机,不在Docker默认网络中,无法解析容器名称,必须使用宿主机映射的127.0.0.1:3000地址。
  • 测试验证:方案A访问https://flex-test.xyz/.well-known/mercure,方案B访问https://flex-test.xyz/mercure,成功时会返回Mercure的JSON响应(如{"type":"mercure.hub","mercure":"https://flex-test.xyz/.well-known/mercure"})。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 07:15:25