无法为多个 API 设置自定义路径。(Socket.IO + ExpressJS + Nginx)

huangapple go评论69阅读模式
英文:

Unable to set custom paths for multiple APIS. (Socket.IO + ExpressJS + Nginx)

问题

我正在尝试在单个VPS上设置多个API并通过Nginx提供它们。
我希望将它们都放在不同的子位置,就像下面的示例一样:


Express远程路径:

[myhost].com/apps/app1/api
[myhost].com/apps/app2/api

Express本地路径:

localhost:[app1端口]
localhost:[app2端口]

Socket.IO远程路径:

[myhost].com/apps/app1/api/socket.io
[myhost].com/apps/app2/api/socket.io

Socket.IO本地路径:

localhost:[app1端口]/socket.io
localhost:[app2端口]/socket.io

Express按预期工作,我能够在本地访问它,就像这样:
curl -ivL http://localhost:[app1端口]

但是,由于Socket.IO的'path'属性,Socket.IO仅在以下路径上响应:
curl -ivL http://localhost:[app1端口]/apps/app1/api/socket.io/?EIO=4

没有Socket.IO的'path',我无法远程访问它。

  ......
  path: "/apps/app1/api/socket.io", <-- 这个 'path' 属性
});

Nginx配置:

  ......
  location /apps {
    index _;
    autoindex on;

    location /apps/app1/api/ {
      proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header    Host             $http_host;
      proxy_pass          http://localhost:[app1端口];
      proxy_http_version  1.1;
      proxy_set_header    Upgrade          $http_upgrade;
      proxy_set_header    Connection       "upgrade";
    }
    location /apps/app1 {
      index index.html;
    }
    ....

我如何在本地Socket.IO中去掉 /apps/app1/api 部分,而不丧失远程访问?
谢谢。

英文:

I'm trying to set up multiple APIs on a single VPS and serve them through Nginx.
I want to have all of them in separate sub-locations like the example below:


Express remote paths:

[myhost].com/apps/app1/api
[myhost].com/apps/app2/api

Express local paths:

localhost:[app1 port]
localhost:[app2 port]

Socket.IO remote paths:

[myhost].com/apps/app1/api/socket.io
[myhost].com/apps/app2/api/socket.io

Socket.IO local paths:

localhost:[app1 port]/socket.io
localhost:[app2 port]/socket.io

Express works as desired, I am able to access it locally like this:
curl -ivL http://localhost:[app1 port]

But, because of Socekt.IO 'path' property, Socket.IO responses only on:
curl -ivL http://localhost:[app1 port]/apps/app1/api/socket.io/?EIO=4

Without Socket.IO 'path' I am not able to access it remotely.

const ioServer = new socketIo.Server(httpServer, {
  .....
  path: &quot;/apps/app1/api/socket.io&quot;, &lt;---- this &#39;path&#39; property
});

Nginx configuration:

  ......
  location /apps {
    index _;
    autoindex on;

    location /apps/app1/api/ {
      proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header    Host             $http_host;
      proxy_pass          http://localhost:[app1 port];
      proxy_http_version  1.1;
      proxy_set_header    Upgrade          $http_upgrade;
      proxy_set_header    Connection       &quot;upgrade&quot;;
    }
    location /apps/app1 {
      index index.html;
    }
    ....

How can I get rid off /apps/app1/api part on localhost Socket.IO without losing the remote access?
Thanks

答案1

得分: 0

好的,答案很简单。只需在 proxy_pass 中使用完整的 URI(带有尾部斜杠),否则该数值将被忽略。

如果在 proxy_pass 中未指定 URI,则请求 URI 将以与客户端发送原始请求时相同的形式传递到服务器,或在处理更改后的 URI 时将传递完全规范化的请求 URI:

location /some/path/ {
  proxy_pass http://127.0.0.1;
}

来自 Nginx 文档
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

英文:

Ok, so the answer is simple. Just use a full URI (with a trailing slash) in proxy_pass, otherwise the value will be ignored.

> If proxy_pass is specified without a URI, the request URI is passed to
> the server in the same form as sent by a client when the original
> request is processed, or the full normalized request URI is passed
> when processing the changed URI:
>
> location /some/path/ {
> proxy_pass http://127.0.0.1;
> }

from Nginx documentation
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

huangapple
  • 本文由 发表于 2023年2月16日 10:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定