WebSocket通信的反向代理配置
AI 摘要salmon-ai
本文
教程
我们使用socket直接填写正确的socket地址,即可连接,如下:
js
new WebSocket(`ws://127.0.0.1:10006/backend/admin/websocket?userId=${userId}`);
但我们如果想通过反向代理连接socket,我们首先修改连接地址,让其请求到我们代理监听的端口,代理才会帮我们转发到后端(注意:生产环境 ws 得改为 wss )
js
new WebSocket(`ws:${location.host}/backend/websocket?userId=${userId}`);
Vue的反向代理配置
js
["/backend/websocket"]: {
target: `http://localhost:10006/`,
changeOrigin: true,
ws: true, //是否启用websocket
pathRewrite: {
['^/backend']: '/backend/admin'
}
},
如果部署到服务端,Nginx关于socket的代理配置
nginx
location /backend/websocket {
rewrite ^/backend/(.*)$ /backend/admin/$1 break;
proxy_pass http://192.168.0.177:10006;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
评论
0 条—
还没有评论,来说第一句