linux服务器上websocket通信返回403forbidden。
环境:
- linux服务器
- nginx代理服务器
- webSocket通信技术
项目中为了实现实时通讯,采用了webSocket。WebSocket协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信——可以通俗的解释为服务器主动发送信息给客户端。(百度百科)
在本地测试通过js调用搭建通讯管道成功(我喜欢叫通信管道,比较形象)。但是将项目发布到linux服务器上就之后通过js访问返回403错误,信息是forbidden。理解是服务器拒绝处理。我关了linux服务器防火墙等等都没有效果,最后发现是因为nginx代理服务器的原因,因为linux服务器上发布了多个项目,所以需要通过nginx来进行服务器和域名映射。
查找了资料,费了好大力气才找到:
在nginx.conf中加上
server {
listen 80;
server_name www.baidu.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.jsp;
proxy_pass http://www.baidu.com;
proxy_pass_header X-XSRF-TOKEN;
proxy_set_header Origin "http://www.baidu.com";
# enables WS support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
记录一下,防止自己忘记,也 给大家找找思路。