nginx代理socket笔记

修改 nginx.conf 文件

event{
	...
}

stream{
    upstream abc {
       server 127.0.0.1:3000;
    }
    server {
        listen 3001;
        proxy_connect_timeout 1s;
        proxy_timeout 5s;
        proxy_pass abc;
    }
}

http {

	map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    ...
}

主要是新增了 steam 块和 map 部分的代码

修改对应站点的vhost配置文件中 添加

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server{
	....

	location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 600s;
        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        client_max_body_size  30m;
    }

	....
}

使用 curl测试 websocket

curl --no-buffer -H 'Connection: keep-alive, Upgrade' -H 'Upgrade: websocket' -v -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: websocket' http://websocket地址 ws | od -t c

问题

  1. 返回 Session ID unknown

nodejs连接socketio负载均衡报错Session ID unknown 中看到

原本以为是因为多台socket服务器没有连接到同一个io adapter导致session id互相不认识

我这里是因为2台socket服务器运行导致的,停止一个socket运行服务就解决了。

References

posted @ 2021-02-18 17:33  五毛钱的饼  阅读(1036)  评论(0编辑  收藏  举报