After hosting couple of Minecraft servers for friends and family. I was giving them an IP and port to the servers, after creating couple different server some vanilla and modded servers. Every time creating a new server, I needed to configured a port forward rule, on router. I was starting to get annoyed by it, and having specifying port number.
I want have a neater IP without specifying a port, and using domain names specify specific server, and if I want to run 2 dedicated server and have multiple Minecraft server, I had to remember, what public ports were assign to which machine.
Install HAProxy using your distribution’s package manager:
- Ubuntu and Debian:
apt install haproxy
- Arch Linux:
pacman -S haproxy - Fedora Linux:
dnf install haproxy
After installing HAProxy, navigate to the configuration directory:
cd /etc/haproxy/
It’s a good idea to back up the default configuration file before making changes:
mv haproxy.cfg haproxy.cfg.bak
Now create a new haproxy.cfg file.
frontend minecraft
bind *:25565
mode tcp
tcp-request inspect-delay 5s
acl is_mc_vanilla payload(4,21) -m sub vanilla.example.home
acl is_mc_creative payload(4,22) -m sub creative.example.home
acl is_mc_modded payload(4,20) -m sub modded.example.home
tcp-request content accept if is_mc_vanilla || is_mc_creative || is_mc_modded
use_backend vanilla_backend if is_mc_vanilla
use_backend creative_backend if is_mc_creative
use_backend modded_backend if is_mc_modded
backend mc_vanilla_backend
server mc_vanilla 192.168.1.5:25565
backend mc_creative_backend
server mc_creative 192.168.1.5:25566
backend mc_modded_backend
server mc_modded 192.168.1.7:25565
The function payload(Offset, Length). takes two parameters:
- Offset: The starting position of the scan.
- Length: Determinant how many bytes will be scanned.
Note:
When calculatingLength, you must add +1 byte to the domain length.
This ensures the full domain is scanned correctly.
Now after configuring proxy, now If I add a new server in the future, I don’t have to remember what port was assign on my router, I just need to remember what domain was assign.
Here some option quick characters counter
Linux and MacOS bash:
echo "example.com" | wc -c
Windows PowerShell:
echo (("example.com".Length + 1))
Python option:
print(len("example.com")+1)