Nginx配置、WebSocket代理、位置、if。

3

我有一个关于nginx配置的非常具体的问题:

我需要nginx实现以下功能:

  • 在“/”路径下代理websocket连接
  • 对标准http请求以index.html作为响应返回 @ /

这是我能接近的最佳方案:

  location = / {
   if ($http_upgrade != "websocket") {
    # Here lies my problem:
    # This returns a http: 302 where i just need it to return the contents
    # of index.html
    return https://admin.permaconn.com/index.html;
   }

   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_pass http://localhost:8080;
  }

我正在对我的应用进行结构性改变,从使用nodejs作为前置代理转而使用nginx作为前端代理。

由于已安装在设备上的众多程序(也称为遗留系统)期望这种行为,因此我必须以这种方式配置nginx。

1个回答

11
你在错误地使用return指令。
location = / {
    index index.html;

    if ($http_upgrade = "websocket") {
        proxy_pass http://localhost:8080;  
    }

    proxy_http_version 1.1;
    proxy_set_header Upgrade websocket;
    proxy_set_header Connection upgrade;
}

文档:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接