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

Laravel-echo-server生产环境WebSocket 502 Bad Gateway故障求助

Troubleshooting 502 Bad Gateway for Laravel Echo Server with Cloudflare

Hey there, I’ve run into this exact 502 Bad Gateway issue with Laravel Echo Server + Cloudflare before, so let’s break down the most likely fixes based on your setup:

1. Fix Cloudflare’s WebSocket & SSL Settings

First, Cloudflare often is the culprit here because it sits between your users and your server:

  • Enable WebSockets: Head to Cloudflare’s dashboard → Network tab, make sure the WebSockets toggle is turned on. If this is off, Cloudflare will block all WebSocket connections outright.
  • Match SSL Modes: Your Nginx uses SSL (port 8443 with certs), so set Cloudflare’s SSL mode to Full or Full (Strict) (under SSL/TLS → Overview). Mismatched SSL modes (like Cloudflare using Flexible while your server uses Full) will cause handshake errors leading to 502s.
  • Bypass Cache for WebSocket Path: Create a Page Rule in Cloudflare for yourdomain.tld/ws/* (or /socket.io/* if you didn’t customize the path) with these settings:
    • Cache Level: Bypass
    • Disable Apps: On
    • Disable Performance: On
      WebSocket connections can’t be cached, so this prevents Cloudflare from messing with the connection.

2. Adjust Laravel Echo Server Configuration

Your current config has a couple of settings that might cause issues in production:

  • Set host to 0.0.0.0 instead of empty: This makes the Echo Server listen on all network interfaces, ensuring Nginx can reliably reach it (even if there’s a network binding issue).
  • Turn off devMode in production: Dev mode adds extra logging but can sometimes introduce unexpected behavior with production proxies.
  • Double-check apiOriginAllow: While * works, specifying your actual domain (https://yourdomain.tld) is more secure and avoids any edge cases with origin checks.

Update your laravel-echo-server.json and restart the service, then verify it’s listening correctly with:

netstat -tulpn | grep 6001

You should see something like 0.0.0.0:6001 in the output.

3. Fix Nginx Proxy Configuration

A common mistake here is mismatched path routing and missing proxy headers:

  • Check the Location Path: Laravel Echo Server uses /socket.io by default for Socket.io connections. If you didn’t customize the path in your frontend Echo setup, your Nginx location should be /socket.io instead of /ws. If you did set path: '/ws' in your frontend, keep /ws but make sure the Echo Server is configured to handle that path.
  • Add Critical Proxy Headers: Update your Nginx location block with these headers to pass correct request info to Echo Server:
    location /socket.io { # or /ws if you customized it
        proxy_pass http://127.0.0.1:6001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        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;
    }
    
  • Check Nginx Error Logs: Look into /var/log/nginx/domain.tld/ws.log—it will tell you exactly why the 502 is happening (e.g., "connection refused" if Echo Server isn’t running, or "no route found" if the path is wrong).

4. Test Connections Step-by-Step

Isolate where the failure is happening:

  1. Test Echo Server Directly: On your server, run:
    websocat ws://127.0.0.1:6001/socket.io/?EIO=4&transport=websocket
    
    If this connects successfully, the Echo Server itself is working.
  2. Test Nginx Proxy: Still on your server, run:
    websocat ws://localhost:8443/socket.io/?EIO=4&transport=websocket
    
    (Adjust the path to /ws if needed.) If this fails, the problem is in Nginx. If it works, the problem is with Cloudflare.

5. Check Version Compatibility

Your Laravel Echo Server (1.6.1) and Node.js (12.19.0) are technically compatible, but older versions can have hidden Socket.io bugs. Try upgrading Laravel Echo Server to the latest stable version (follow official docs to avoid breaking changes) to rule out version-related issues.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 16:02:33