0

nginx使用以下内容与我的 websocket 服务器一起工作:

server {
    listen 80;
    server_name streams.domain.com;
    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://192.168.1.52:80;
    }
}

但是,我需要根据查询字符串参数更改目的地。我将其更改为以下内容:

server {
    listen 80;
    server_name streams.domain.com;
    location / {
        if ( $arg_serverId = 1562182 ) { 
            return 101 http://192.168.1.52:80; 
        }
    }
}

现在我得到了错误Error during WebSocket handshake: 'Upgrade' header is missing

所以我尝试使用add_header,但使用下面的配置我只是得到ERR_CONNECTION_TIMED_OUT

server {
    listen 80;
    server_name streams.domain.com;
    location / {
        if ( $arg_serverId = 1562182 ) { 
            add_header 'Upgrade' $http_upgrade;
            add_header 'Connection' $connection_upgrade;
            return 101 http://192.168.1.52:80; 
        }
    }
}

那么如何使用$arg_条件并传递 websocket 所需的升级标头?

4

1 回答 1

0

我最终让它与map. 使用如下所示的 URLhttp://stream.domain.com?serverId=1001效果很好:

http {
    map $arg_serverId $streamdestination {
        1001 http://192.168.1.52:80; 
        1002 http://192.168.1.51:80; 
        1003 http://192.168.1.50:80; 
    }
    server {
        listen 80;
        server_name streams.domain.com;
        location / {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_pass $streamdestination;
        }
    }
}
于 2019-02-21T03:56:53.427 回答