Nginx Streams

I'm pretty new to Nginx and managing subdomains etc. and this doesn't exactly have something to do with Cloudflare but I'm not sure where else to ask. This is my config for trying to setup minecraft servers:
stream {
map $ssl_preread_server_name $target {
alpha 192.168.178.2:27000; # I wasn't sure if I should reference the subdomain or the full name `alpha.example.com` but either way this didn't work.
beta 192.168.178.2:27001;
gamma 192.168.178.2:27002;
delta 192.168.178.2:27003;
epsilon 192.168.178.2:27004;

default 192.168.178.2:27000;
}

server {
listen 25565;
listen [::]:25565;

ssl_preread on;
proxy_connect_timeout 1s;
proxy_timeout 3;

proxy_pass $target;
}
}
stream {
map $ssl_preread_server_name $target {
alpha 192.168.178.2:27000; # I wasn't sure if I should reference the subdomain or the full name `alpha.example.com` but either way this didn't work.
beta 192.168.178.2:27001;
gamma 192.168.178.2:27002;
delta 192.168.178.2:27003;
epsilon 192.168.178.2:27004;

default 192.168.178.2:27000;
}

server {
listen 25565;
listen [::]:25565;

ssl_preread on;
proxy_connect_timeout 1s;
proxy_timeout 3;

proxy_pass $target;
}
}
My idea is to have n amound of CNAMEs and SRV records that all listen at 25565 (minecrafts default port) and forward to their respective subdomain on my server. that server then listens on said port 25565 and should forward it to the specific minecraft server depending on the subdomain. e.g.: alpha.example.com > 192.168.178.2:27000 beta.example.com > 192.168.178.2:27001 ... other.example.com > 192.168.178.2:27000 (default should either be the first one or no connection at all) currently everything coming in at 25565 resolves to the default being 192.168.178.2:27000 which isn't great. my motivation being that I had a setup before where the client would have to specify different ports if it wasn't the default one like beta.example.com:25566 which I'd like to avoid. Anyone have an idea how i could implement this/ where i could find more information on this topic? Thanks in advance
29 Replies
マオ
マオOP2mo ago
On another note, I was also thinking of reusing the CNAME records by creating new SRV records but for different ports, so that i can reuse names like alpha.example.com but listening on some port 12345 which I would then resolve similarly to minecraft to some other game server like ARK, but first id like to get it to work for minecraft. Is this possible though?
Laudian
Laudian2mo ago
If you create the correct SRV record, clients don't need to enter the port. I don't see how what you've explained above would work.
マオ
マオOP2mo ago
How would a correct SRV record look like?
Laudian
Laudian2mo ago
It would have the correct port
マオ
マオOP2mo ago
currently I have something like _minecraft._tcp.alpha.example.com On port 25565 targeting alpha.example.com
Laudian
Laudian2mo ago
The SRV record tells minecraft what port the server on alpha is using
マオ
マオOP2mo ago
The port I enter in the SRV record is the one the server is using and not what the client will use to connect?
Laudian
Laudian2mo ago
both are the same?
マオ
マオOP2mo ago
Hmm no because I have for example alpha running on 27000 beta on 27001 etc
Laudian
Laudian2mo ago
Yes?
マオ
マオOP2mo ago
But the user will always be incoming on 25565 So how do I link them correctly
Laudian
Laudian2mo ago
no why will the user connect to 25565?
マオ
マオOP2mo ago
because they just input the address without specifying the port, so they use the standard port of 25565
Laudian
Laudian2mo ago
no
マオ
マオOP2mo ago
At least that’s what I’m hoping to achieve
Laudian
Laudian2mo ago
thath's why you have an srv record to tell minecraft which port to use
マオ
マオOP2mo ago
Hmm ok so I change the SRV to port 27000 for example Then the user goes to alpha.example.com, without specifying the port, and my server is listening at 27000 Would this work?
Laudian
Laudian2mo ago
yes
マオ
マオOP2mo ago
Interesting, didn’t know SRVs worked like that Btw I called it _minecraft._tcp.alpha because I saw others do it, is that just a naming convention/ preference or is the name important?
Laudian
Laudian2mo ago
for sub.example.com, minecraft checks for an srv on _minecraft._tcp.sub.example.com
マオ
マオOP2mo ago
Just to make sure, there’s no way I can route on my reverse proxy instead, routing depending on subdomain, I’d prefer only opening one port on my router if possible
Laudian
Laudian2mo ago
no not without plugins because you don't know what subdomain someone used to connect
マオ
マオOP2mo ago
unfortunate how feasible is installing/ using the plugins? though imma first try it with the SRV records real quick
Laudian
Laudian2mo ago
installing? I doubt on that does exactly this exists, so you'd need to program it first.
マオ
マオOP2mo ago
ah lol yeah ok fair enough wow that actually worked with the SRVs, thank you! now if i wanted to reuse the CNAME alpha.example.com id just need to create a new SRV on a different port and link that to some different game server, right?
Laudian
Laudian2mo ago
or change the old srv^^
マオ
マオOP2mo ago
would both at the same time work? 👀 alpha.example.com in minecraft would go to the minecraft server and the same address would go to for example an ark server if i have two SRVs setup? because if so thatd be pretty neat
Laudian
Laudian2mo ago
yes But I believe Ark doesn't support SRV records? You can use the same subdomain for every game that supports srv records, but only one game that doesn't
マオ
マオOP2mo ago
ah fair, i didnt check if ark does but that makes sense thanks so much for the help! you probably saved me hours if not days of trying to get this to work ^^;

Did you find this page helpful?