我正在使用 netty 4.0.20 我想使用不同的 url 在同一端口上创建不同的 websocket 服务器
例如, wss://localhost:1234/PathA
wss://localhost:1234/PathB
wss://localhost:1234/PathC
这可能吗?
最佳答案
是的,这可以通过使用反向代理来实现,这可以通过 Nginx 来完成。
这将需要在您的设置中添加一台额外的服务器。
首先,您必须将每个服务器设置为监听不同的端口,然后您需要前端服务器监听所需的公共(public)端口(在您的情况下,这是 1234)。
假设您有以下服务器
- Nginx 监听
0.0.0.0:1234
- Netty 服务于
/PathA
并监听0.0.0.0:1235
- Netty 服务
/PathB
并监听0.0.0.0:1236
- Netty 服务
/PathC
并监听0.0.0.0:1237
现在您要做的就是编写一个 Nginx 配置文件,将连接从 HTTP 升级到 Websocket,然后将每个路径反向代理到其相应的服务器。下面是一个可以为您完成这项工作的示例配置文件。
{
listen 1234;
server_name localhost;
location ~PathA/$ {
proxy_pass http://localhost:1235;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
location ~PathB/$ {
proxy_pass http://localhost:1236;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
location ~PathC/$ {
proxy_pass http://localhost:1237;
proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "upgrade";
}
}
关于websocket - 使用一个端口号创建多个websocket服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781886/