nginx代理websocket指客户端通过websocket访问服务的A端口,nginx可以将该websocket连接与通信代理转发到后端的B端口,其配置文件如下设置
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /socket {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /socket2 {
proxy_pass http://127.0.0.1:9999;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
后端存在两个ws服务,分别监听8888与9999端口。
客户端通过ws://127.0.0.1:8081/socket 可与后端的8888端口通信
客户端通过ws://127.0.0.1:8081/socket2 可与后端的9999端口通信