1
我有一个游戏在我的服务器上运行。 当用户请求它时,我的服务器上的一个新进程将启动并开始侦听8001-8100范围内的随机端口上的websocket连接。Nginx条件动态代理
在客户端上我想连接到游戏。
var port = 8044; // <- Client is given this number when creating a new game
connection = new WebSocket("wss://XXX/websocket?port=" + port);
我的服务器是一个nginx反向代理。我想要做这样的事情: (我需要一个工作替换它,下面要说明这一点)
server {
...
location /websocket/ {
if(isNumber($port) && $port <= 8100 && $port >= 8001 {
proxy_pass http://localhost:$port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
} }
原因这些反向代理把戏是能够使用默认的443端口,这意味着客户不会遇到防火墙问题。 Othwerwise我只是直接连接到客户端的正确端口。
任何想法?
不幸的是'proxy_pass http:// localhost:$ arg_port;'不起作用。只要我使用它而不是数字,我只收到错误502. – user2089648
嗯。它为我工作。错误日志中有什么有用的吗? –
'没有解析器定义来解析localhost' - 好吧,用127.0.0.1替换它的作品。不太确定,为什么localhost与'$ arg_port'结合不起作用,但至少我现在已经运行了,谢谢。 – user2089648