Laravel-echo-server生产环境WebSocket 502 Bad Gateway故障求助
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
hostto0.0.0.0instead 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
devModein 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.ioby default for Socket.io connections. If you didn’t customize thepathin your frontend Echo setup, your Nginxlocationshould be/socket.ioinstead of/ws. If you did setpath: '/ws'in your frontend, keep/wsbut 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:
- Test Echo Server Directly: On your server, run:
If this connects successfully, the Echo Server itself is working.websocat ws://127.0.0.1:6001/socket.io/?EIO=4&transport=websocket - Test Nginx Proxy: Still on your server, run:
(Adjust the path towebsocat ws://localhost:8443/socket.io/?EIO=4&transport=websocket/wsif 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

