英文:
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: "/apps/app1/api/socket.io", <---- this 'path' 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 "upgrade";
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论