前言
nginx支持ws协议的配置
配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream BackendIotWs {
server 10.0.0.110:7004 weight=1;
}
server {
listen 80;
server_name ws.iot.com;
location / {
try_files $uri $uri/ /index.html last;
root "/usr/local/openresty/nginx/std/manage/";
index index.html index.htm;
}
location /ws/iot {
proxy_pass http://BackendIotWs
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 3600s;
proxy_connect_timeout 86400;
proxy_ignore_client_abort on;
}
}
测试
- 访问地址:ws://ws.iot.com/ws/iot
- 测试地址:http://coolaf.com/tool/chattest,测试正常结果,如下所示

本文档详细介绍了如何配置Nginx以支持WebSocket协议。通过设置`map`指令、`upstream`和`location`块,实现了对WebSocket连接的代理。配置完成后,通过ws://ws.iot.com/ws/iot进行访问,并使用http://coolaf.com/tool/chattest进行测试,结果显示正常。
3351

被折叠的 条评论
为什么被折叠?



