nginx部署vue使用websocket
时间: 2023-11-26 11:04:14 浏览: 103
在nginx部署vue使用websocket时,你需要进行一些配置。首先,在开发环境中,你可以在vue.config.js文件中配置devServer,使用proxy来代理websocket请求。具体的配置可以参考以下代码示例:
```
devServer: {
proxy: {
'/wsct': {
target: '真正开发环境的websocket地址',
changeOrigin: true,
pathRewrite: {
'^/wsct': '',
},
ws: true
}
}
}
```
在线上环境中,你需要在nginx配置中添加一些规则,以支持websocket请求。你可以参考以下代码示例:
```
server {
location /wsct {
proxy_pass '线上websocket地址';
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
```
这样,你就可以在vue项目中使用websocket进行通信了。另外,如果你想进行群聊,可以使用不同的访问地址来区分不同的用户,例如ws://localhost:80/ws/socket/lhz/和ws://localhost:80/ws/socket/zzh/。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [vue项目部署在nginx,vue开发环境代理websocket请求,线上nginx代理websocket请求。](https://blog.csdn.net/weixin_43790802/article/details/127285321)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [django部署:django+uwsgi+nginx+vue+websocket](https://blog.csdn.net/weixin_46371752/article/details/130568190)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [websocket消息推送_windows+nginx+wss+https.rar](https://download.csdn.net/download/qq_31648519/12596783)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文