美文网首页
nginx 代理 websocket

nginx 代理 websocket

作者: 李其伟 | 来源:发表于2017-09-06 17:55 被阅读0次

在nginx.conf中添加,具体可以查看下面官方地址

http {

map $http_upgrade $connection_upgrade {

default upgrade;

''      close;

}

server {

...

location /chat/ {

proxy_pass http://backend;     #被代理服务器的访问地址

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection $connection_upgrade;

}

}

注意遇到问题

现象 :代理访问返回 502错误

解决。如果如上面 location /chat/ {} 这样配置 ,  那么程序中接收websocket数据的服务器地址应该多添加/chat/,相对于不用代理的情况下。

配置nginx代理前访问地址 ws://baidu.com/commend  

配置nginx代理后访问地址 ws://baidu.com/chat/commend   后台websocket也要添加/chat/

官方文档地址 http://nginx.org/en/docs/http/websocket.html

相关文章

网友评论

      本文标题:nginx 代理 websocket

      本文链接:https://www.haomeiwen.com/subject/riyvjxtx.html