M
M
MrPofix2020-10-20 17:09:30
Laravel
MrPofix, 2020-10-20 17:09:30

How to defeat 502 error when working with Laravel-echo-server?

I haven't been able to find a reason for a week now. Works fine on dev server, but not on production.
I am using Cloudflare. I set up a proxy, laravel echo works, it accepts events. But when I try to connect to the socket, I get an error: WebSocketError: Unexpected status code received (502 Bad Gateway)

Environment:
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 configuration:
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";
        }
}


I start Laravel Echo, I see that everything is OK:
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!


I try with curl, it works:
$ curl 127.0.0.1:6001
> OK


I try to connect through the domain:
websocat wss://domain.tld:8443/ws

And I see an error:
websocat: WebSocketError: Received unexpected status code (502 Bad Gateway)


Tried different hosts in laravel-echo-server.json settings (127.0.0.1, localhost, null, 0.0.0.0) - doesn't help.
Read this post https://github.com/tlaverdure/laravel-echo-server/... didn't help either.
I don't even know how to debug this?

Please, help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrPofix, 2020-10-28
@MrPofix

I don't know what the problem is, but I found a solution.
I updated Nginx to 1.19.1, may have installed additional modules when trying to build Nginx from source.
I installed npm not globally, but locally - maybe this also somehow affected.
Warning: this only works if you write location /socket.io.
No other location works, just this one!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question