Nginx - 反向代理具有多个路径的WebSocket服务器

时间:2016-05-18 10:15:56

标签: nginx websocket reverse-proxy

我想我的问题的答案会非常简单,但不幸的是我自己也找不到了!

我在服务器A(192.168.1.1)上运行的Nginx充当服务器B(192.168.1.2)上的WebSocket服务器的反向代理。最后一个服务器正在侦听端口1234并有2个路径,因此我们可以连接到以下地址:

while(1){
  if (blTimeOverFlag == 1)
  {   
    blTimeOverFlag = 0;
    array[filling_cnt] = PORTDbits.RD0;          // Read RD0 and save in array        
     filling_cnt++;                         // Array counter for filling control

     if(filling_cnt >= SIZE){                  // Reached the end of array ?
        filling_cnt = 0;
        printf("%s\n", array);             // Send data to terminal
        for (j=0; j<SIZE; j++){
            array[j] = '\0';               // Empty array
        } //rof
     } //fi
  }
}           

通过添加以下配置,我已经能够通过代理(ws://192.168.1.1/websocket/path1)成功连接到Path1:

ws://192.168.1.2:1234/path1
ws://192.168.1.2:1234/path2

现在我想使它足够通用以匹配第二条路径(而不是为path2创建类似的规则),我尝试了类似的东西:

location /websocket/path1 {
    proxy_pass http://192.168.1.2:1234/path1;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

但是这样我无法访问WebSocket。

你知道什么是错的吗?

非常感谢你的时间!

1 个答案:

答案 0 :(得分:5)

这很简单,它通过在网址的末尾添加“斜线”来实现:

location /websocket/ {
    proxy_pass http://192.168.1.2:1234/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

我很确定已经尝试过这种方式,但看起来我错了! :)