1

I have a game running on my server. When a user requests it, a new process on my server is started and starts to listen for a websocket connection on a random port in the range 8001-8100.

On the client i want to connect to the game.

var port = 8044; // <- Client is given this number when creating a new game
connection = new WebSocket("wss://XXX/websocket?port=" + port);

My server is a nginx reverse proxy. I want to do something like this: (I need a working replacement for it, the below should illustrate the point.)

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";
  }
} }

Reason for those reverse proxy shenanigans is to be able to use the default 443 port, meaning clients won't have firewall problems. Othwerwise I would just directly connect to the correct port from the client.

Any ideas?

1 Answer 1

1

The port argument is available as $arg_port. It can be checked using a regular expression. For example:

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";
}

A little poetic licence with the port range, but a more complex expression can achieve your precise requirements. I avoid putting anything too complex inside an if block for these reasons.

See this document for more.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately proxy_pass http://localhost:$arg_port; does not work. As soon as I use it instead of a number, I only receive error 502.
Hmm. It worked for me. Is there anything useful in the error log?
no resolver defined to resolve localhost - well, replacing it with 127.0.0.1 works. Not really sure, why localhost in combination with $arg_port does not work, but at least I got it running now, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.