Nginx条件动态代理
我的服务器上正在运行一个游戏。 当用户请求时,我的服务器上会启动一个新进程,并开始侦听8001-8100范围内随机端口上的websocket连接 在客户端上,我要连接到游戏Nginx条件动态代理,nginx,websocket,proxy,Nginx,Websocket,Proxy,我的服务器上正在运行一个游戏。 当用户请求时,我的服务器上会启动一个新进程,并开始侦听8001-8100范围内随机端口上的websocket连接 在客户端上,我要连接到游戏 var port = 8044; // <- Client is given this number when creating a new game connection = new WebSocket("wss://XXX/websocket?port=" + port); var端口=8044;// port参
var port = 8044; // <- Client is given this number when creating a new game
connection = new WebSocket("wss://XXX/websocket?port=" + port);
var端口=8044;// port
参数可用作$arg\u port
。可以使用正则表达式对其进行检查。例如:
location /websocket/ {
if ($arg_port !~ "^80[0-9]{2}$") {
return 403;
}
proxy_pass http://localhost:$arg_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
端口范围有点诗意,但更复杂的表达式可以满足您的精确要求。我避免将任何过于复杂的内容放入的if
块中
有关更多信息,请参阅。不幸的是代理\u passhttp://localhost:$arg_port代码>不起作用。当我使用它而不是数字时,我只收到错误502.Hmm。这对我有用。错误日志中有什么有用的东西吗?没有定义解析程序来解析localhost
-好吧,用127.0.0.1替换它就行了。不太清楚,为什么localhost与$arg_port
组合不起作用,但至少我现在运行了它,谢谢。
location /websocket/ {
if ($arg_port !~ "^80[0-9]{2}$") {
return 403;
}
proxy_pass http://localhost:$arg_port;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}