WebSocket with Laravel-echo-server: 502 Bad gateway

我的未来我决定 提交于 2020-12-13 03:39:18

问题


I can't find a solution for more than a week. Everything works fine on the dev server, but not on production. I have no strength, I appeal to you, colleagues :)

I'm using Laravel-echo-server on Socket.io using CloudFlare. When trying to connect, I get an error: WebSocketError: Unexpected status code received (502 Bad Gateway)

Ubuntu 16.04.7 LTS, Laravel Echo Server: 1.6.1, NodeJs: v12.19.0

laravel-echo-server.json:

    {
    "authHost": "http://localhost",
    "authEndpoint": "/broadcasting/auth",
    "clients": [
        {
            "appId": "...",
            "key": "..."
        }
    ],
    "database": "redis",
    "databaseConfig": {
        "redis": {
            "password": "...",
            "port": "...",
            "db": 0
        },
        "sqlite": {}
    },
    "devMode": true,
    "host": "",
    "port": "6001",
    "protocol": "http",
    "socketio": {
        "transports": ["websocket", "polling"]
    },
    "secureOptions": 67108864,
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": true,
        "allowOrigin": "*",
        "allowMethods": "GET, POST",
        "allowHeaders": "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
    }
}

Nginx host:

    server {
        listen 8443 ssl;
        server_name domain.tld;       
        ssl_certificate /etc/nginx/ssl/my.crt;
        ssl_certificate_key  /etc/nginx/ssl/my.key;
        error_log /var/log/nginx/domain.tld/ws.log info;

        location /ws {
                proxy_pass http://127.0.0.1:6001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
        }
}

After start EchoServer i see:

L A R A V E L  E C H O  S E R V E R

version 1.6.1

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

Ok, next:

$ curl 127.0.0.1:6001
> OK

Next:

websocat wss://domain.tld:8443/ws

And I get the error:

websocat: WebSocketError: Received unexpected status code (502 Bad Gateway)

Laravel echo server is running, I can see its events. Tried changing the host in laravel-echo-server.json (127.0.0.1, localhost, null, 0.0.0.0), in nginx proxy to localhost, 127.0.0.1 - nothing helps. I tried to add upstreams in nginx host, it doesn't help. This information also did not help: https://github.com/tlaverdure/laravel-echo-server/issues/273

Please, help :)


回答1:


I assume this is your issue.

{
  "authHost": "http://localhost" // This should be your domain name
}



回答2:


I don't know what the problem is, but I found a solution. I've updated a lot: updated Nginx to 1.19.1, possibly installed some additional modules. Installed npm not globally, but locally. And it worked ...

Attention: this only works if you write location /socket.io. That was not the point for sure: "authHost": "http://localhost"



来源:https://stackoverflow.com/questions/64428863/websocket-with-laravel-echo-server-502-bad-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!