我让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;
}
}
但是,我需要基于querystring参数更改目标。我将其更改为以下内容:
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所需的Upgrade标头?
答案 0 :(得分:0)
我最终使它与map
一起使用。使用http://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;
}
}
}