Home > OS >  NGINX Problem with Configuring Websocket, no problem with SSE
NGINX Problem with Configuring Websocket, no problem with SSE

Time:09-01

i am working with NGINX configuration right now.

I am stuck at some strange point. NGINX allow me to connect via SSE but not via Websocket.

This is my config:

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

This is error from browser console via WEBSOCKET:

[2022-08-30T07:49:22.667Z] Error: Failed to start the transport 'WebSockets': Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.

This is WEBSOCKET: Browser Error

This is SSE: SSE

##EDIT:

This is section which fail:

try {
                    await this._startTransport(connectUrl, requestedTransferFormat);
                    this.connectionId = negotiate.connectionId;
                    return;
                }
                catch (ex) {
                    this._logger.log(LogLevel.Error, `Failed to start the transport '${endpoint.transport}': ${ex}`);
                    negotiate = undefined;
                    transportExceptions.push(new FailedToStartTransportError(`${endpoint.transport} failed: ${ex}`, HttpTransportType[endpoint.transport]));
                    if (this._connectionState !== "Connecting" /* Connecting */) {
                        const message = "Failed to select transport before stop() was called.";
                        this._logger.log(LogLevel.Debug, message);
                        return Promise.reject(new Error(message));
                    }

###UPDATE

I FOUND THE SOLUTION. MY ANSWER IS CORRECT ANSWER! :)

CodePudding user response:

I am comming back with the solution.

It is easy.

First thing is: You need to add in nginx.conf in http { } section this:

 map $http_upgrade $connection_upgrade 
    {
    default upgrade;
    ''      close;
    }

and this is config:

server {
listen 50150;
    server_name  _;
    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection $connection_upgrade;
        proxy_cache off;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

and you are good to go!

[2022-08-30T10:30:48.900Z] Information: WebSocket connected to ws:~~/~hub?id=Zk-~~.

I hope someone will get use of it.

Best regards

  • Related