Nginx服务器上Laravel Broadcast Redis Socket IO无法运行,求配置方案
Laravel Broadcast + Redis + Socket.IO 部署Nginx服务器配置调整方案
1. Nginx反向代理配置
添加反向代理规则,将WebSocket请求转发到Laravel Echo Server的6003端口,同时处理WebSocket协议升级。在你的Nginx站点配置中添加以下内容:
server { listen 80; listen [::]:80; server_name staging-test.com; # 指向Laravel项目的public目录 root /path/to/your/laravel/public; index index.php index.html; # 处理Laravel Echo Server的WebSocket请求 location /laravel-echo-server { proxy_pass http://localhost:6003; 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; } # Laravel默认路由处理 location / { try_files $uri $uri/ /index.php?$query_string; } # PHP文件处理(根据你的PHP版本调整socket路径) location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; } }
2. 防火墙端口配置
- 若使用反向代理,无需直接暴露6003端口,仅开放80/443端口即可:
ufw allow 80/tcp ufw allow 443/tcp ufw reload
- 若需直接访问6003端口(不推荐),执行:
ufw allow 6003/tcp ufw reload
3. Laravel Echo Server配置修改
编辑laravel-echo-server.json,调整以下关键参数:
{ "authHost": "https://staging-test.com", "authEndpoint": "/broadcasting/auth", "host": "localhost", "port": "6003", "apiOriginAllow": { "allowCors": true, "allowOrigin": "https://staging-test.com", "allowMethods": "GET, POST", "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id" } }
host设为localhost,避免直接绑定公网IPauthHost和apiOriginAllow.allowOrigin设置为你的域名(启用HTTPS需带https://)
4. Laravel应用端配置调整
修改.env文件的广播相关配置:
BROADCAST_DRIVER=redis REDIS_HOST=127.0.0.1 REDIS_PORT=6379 # Echo前端配置 MIX_ECHO_HOST="${APP_URL}" MIX_ECHO_PORT=80 MIX_ECHO_SCHEME=${APP_SCHEME:-http}
调整resources/js/bootstrap.js中的Echo初始化代码:
import Echo from 'laravel-echo'; window.io = require('socket.io-client'); window.Echo = new Echo({ broadcaster: 'socket.io', host: window.location.hostname, path: '/laravel-echo-server/socket.io', auth: { headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content, }, }, });
5. Redis服务检查
确保Redis服务正常运行且可访问:
# 检查Redis状态 systemctl status redis-server # 测试Redis连接 redis-cli ping
若Redis设置了密码,需在.env和laravel-echo-server.json中同步配置REDIS_PASSWORD。
6. SSL配置(可选但推荐)
若启用HTTPS,添加Nginx SSL配置并更新相关参数:
server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name staging-test.com; ssl_certificate /path/to/your/cert/fullchain.pem; ssl_certificate_key /path/to/your/cert/privkey.pem; # 复用之前的反向代理规则 location /laravel-echo-server { proxy_pass http://localhost:6003; 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; } # Laravel路由及PHP处理规则... } # HTTP重定向到HTTPS server { listen 80; listen [::]:80; server_name staging-test.com; return 301 https://$host$request_uri; }
同时更新.env中的APP_URL为https://staging-test.com,MIX_ECHO_SCHEME为https。
内容的提问来源于stack exchange,提问作者Pargat Singh
相关产品推荐
相关产品推荐

