Nginx 配置 Websocket 實測成功
ws和wss的配置完全一樣,區別是放置的位置,ws放置到監聽80端口的server,wss放置到監聽443端口的server
現狀:
使用uniapp和springboot前後端實現即時通訊
實測成功:
1、我的前端url示例:
ws://192.168.0.176:7394/prod-api/websocket/1231114816408653824/1
2、我的springboot後端代碼
@Component
@Slf4j
@ServerEndpoint("/websocket/{userId}/{type}")
public class WebSocketServer {
xxx //需要這塊內容的可以私聊給
}
話不多說直接上配置:
# websocket 支持
location /prod-api/websocket/ {
proxy_pass http://127.0.0.1:7394; //代理到本地,如果是其他服務器,適時修正
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400; // 設置超時時間(nginx默認60秒超時,這裏需要延長超時時間)
}
網上其他帖子上說要加Map的,其實不是必須的。