为了方便客户端与服务端的双向通讯,现代网站越来越多的使用Websocket做为服务,现在我们就来介绍下nginx 的ws代理实现。
假设当客户端访问http://domain/wsmessage时,update成ws://127.0.0.1:10000,其配置如下:
location /wsmessage{ # 与 V2Ray 配置中的 path 保持一致
proxy_redirect off;
proxy_pass http://127.0.0.1:10000;#假设WebSocket监听在环回地址的10000端口上
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
# Show realip in v2ray access.log
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
重启nginx试试看。