nginx配置,websocket代理,位置,如果

Rod*_*ist 2 config nginx websocket

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

我需要nginx:

  • 代理websocket连接@ /
  • 使用index.html响应标准http请求@ /

这是我能得到的最接近的:

  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;
  }
Run Code Online (Sandbox Code Playgroud)

我正在使用我的应用程序进行结构更改,使用nodejs作为前端代理,将nginx用作前端代理.

我必须以这种方式配置nginx,因为许多程序已经安装了设备(也就是遗留设备)的预期行为.

VBa*_*art 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;
}
Run Code Online (Sandbox Code Playgroud)

文档: