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

Docker+Nginx部署Mercure遇502 Bad Gateway错误求助

Symfony集成Mercure生产环境HTTPS下502 Bad Gateway错误排查与解决

问题背景

维护他人搭建的Symfony项目,集成Mercure后本地环境运行正常,但在启用HTTPS的生产环境中,解决CORS问题后出现502 Bad Gateway错误。

相关配置

docker-compose.yml(Mercure服务部分)

dj_mercure:
  image: dunglas/mercure
  container_name: dj_mercure
  restart: unless-stopped
  ports:
    - "3000:3000"
  networks:
    - core
  environment:
    SERVER_NAME: ':3000'
    MERCURE_PUBLISHER_JWT_KEY: '${MERCURE_SECRET}'
    MERCURE_SUBSCRIBER_JWT_KEY: '${MERCURE_SECRET}'
    MERCURE_EXTRA_DIRECTIVES: |
      cors_origins https://${HOST}
      use_forwarded_headers "1"
  volumes:
    - caddy_data:/data
    - caddy_config:/config

networks:
  core:
    driver: bridge

volumes:
  caddy_data:
  caddy_config:

环境变量HOST值为dev.mysite.fr

Nginx default.conf(新增Mercure代理段)

server {
    listen 80 default_server;

    server_name localhost;
    root /var/www/html/public;
    index index.php index.html index.htm;

    location /.well-known/mercure {
        proxy_pass http://127.0.0.1:3000;
        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;
    }

    # 原有Nginx配置省略...
}

前端订阅代码

const url = new URL('https://dev.mysite.fr/.well-known/mercure');
url.searchParams.append('topic', "http://somesite.local/xyz");
eventSource = new EventSource(url);
eventSource.onmessage = event => { 
  // 处理逻辑
}

Nginx错误日志

2022/07/25 13:46:59 [error] 44#44: *24 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.5, server: localhost, request: "GET /.well-known/mercure?topic=http%3A%2F%2Fsomesite.local%2Fxyz HTTP/1.1", upstream: "http://127.0.0.1:3000/.well-known/mercure?topic=http%3A%2F%2Fsomesite.local%2Fxyz", host: "dev.mysite.fr", referrer: "https://dev.mysite.fr/fr/admin/dashboard"

排查与解决步骤

1. 修正Nginx代理目标地址

错误日志显示Nginx尝试连接127.0.0.1:3000被拒绝,原因是Docker容器拥有独立网络栈,Nginx容器内的127.0.0.1指向容器自身,而非宿主机。由于Mercure与Nginx同属core Docker网络,直接使用Mercure服务名即可通信:

修改Nginx配置中的proxy_pass:

location /.well-known/mercure {
    proxy_pass http://dj_mercure:3000; # 替换为Mercure服务名
    # 其余配置保持不变
}

2. 调整Nginx server_name匹配规则

当前Nginx的server_name为localhost,但请求的host是dev.mysite.fr,可能导致请求匹配异常。修改为:

server {
    listen 80 default_server;
    server_name dev.mysite.fr; # 匹配实际域名
    # 其余配置保持不变
}

3. 验证Mercure容器状态

执行命令检查Mercure容器是否正常运行:

docker-compose ps dj_mercure

若容器未启动,查看日志排查启动失败原因:

docker-compose logs dj_mercure

4. 重启服务使配置生效

修改配置后,重启Nginx和Mercure容器:

docker-compose restart <nginx-service-name> dj_mercure

(注:替换<nginx-service-name>为实际Nginx服务在docker-compose.yml中的名称,如dj_nginx)

5. 验证网络连通性(可选)

进入Nginx容器,测试与Mercure容器的连通性:

docker exec -it <nginx-container-name> ping dj_mercure

若能正常ping通,说明网络通信正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:54:17