84

I have an old computer which I converted into a Minecraft server. I have 2 Minecraft servers running simultaneously, one on port 25565 (default) and one on port 25566.

I bought the domain something.example and pointed it to my server. Right now, in the game you type something.example to get into the first server and something.example:25566 to get into the other server.

Is there a way to set one.something.example to point to the first server and two.something.example to point to the second server? I own the (centos) server, have root access, and everything else. The domain is controlled by no-ip if that makes a difference. I know that DNS has no relationship to port numbers but if there a program I can install to make this work?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
alecwhardy
  • 2,698
  • 6
  • 27
  • 33

6 Answers6

117

If you have access to SRV Records, you can use them to get what you want :)

E.G

A Records

Name: mc1.domain.example
Value: <yourIP>

Name: mc2.domain.example
Value: <yourIP>

SRV Records

Name: _minecraft._tcp.mc1.domain.example
Priority: 5
Weight: 5
Port: 25565
Value: mc1.domain.example

Name: _minecraft._tcp.mc2.domain.example
Priority: 5
Weight: 5
Port: 25566
Value: mc2.domain.example

then in minecraft you can use

mc1.domain.example which will sign you into server 1 using port 25565

and

mc2.domain.example which will sign you into server 2 using port 25566

then on your router you can have it point 25565 and 25566 to the machine with both servers on and Voilà!

Source: This works for me running 2 minecraft servers on the same machine with ports 50500 and 50501

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
MRVDOG
  • 1,717
  • 2
  • 13
  • 20
  • What if I have an alias that's routing *.mysite.com to mysite.cloudprovider.com? I need the wildcard b/c any number of sub-domains need to go to one site. However, I have one special name that I need to go to another site, that's running on a specific port. – Sean Sep 17 '14 at 21:09
  • A note that this is an implementation specific feature. SRV records require the software to do the lookup. – PsyKzz Jan 02 '16 at 18:11
  • 4
    I don't believe this works on browsers though. So you can't use it to direct a subdomain to a specific port in your browser for example. See http://stackoverflow.com/questions/9063378/why-do-browsers-not-use-srv-records. – jrel Jan 16 '16 at 19:58
  • But how does look like on CloudFlare exactly? I've tried to set this up and it looks like this https://goo.gl/7H6H2D and it hasn't changed anything. slack.newspace.nyc just redirects to discuss.newspace.nyc – Mikeumus Feb 13 '16 at 02:07
  • 1
    how about when using godaddy? – xGeo Jun 23 '17 at 10:49
  • 4
    Could you elaborate on how to construct a SRV record like `_minecraft._tcp.mc1.domain.com`? Here's how I understand it: `_something._protocol.subdomain.domain.tld`. What does the `_something` in that example do? Is it just a friendly name or does it have some sort of relevancy that makes it work? – Pyroglyph Nov 09 '17 at 13:37
  • 1
    Awesome thanks so much for your answer. Trying to google for how to redirect a port to subdomain comes up with what feels like a 1000 port forward answers and 500 how to port trigger. This is exactly what I was looking for. – User128848244 Jun 20 '18 at 15:35
  • 1
    Here's a good article specific to namecheap (minecraft related but could really be any app): https://www.namecheap.com/support/knowledgebase/article.aspx/9765/2208/how-can-i-link-my-domain-name-to-a-minecraft-server – cid Dec 21 '18 at 04:58
  • 2
    This will not work for normal websites, which is hit from Browser, as browser doesn't resolve SRV records and will always hit port 80. You can do something like this: https://superuser.com/a/1103011 – Deb Sep 25 '19 at 10:12
111

If you want to host multiple websites in a single server in different ports then, method mentioned by MRVDOG won't work. Because browser won't resolve SRV records and will always hit :80 port. For example if your requirement is:

site1.domain.example maps to domain.example:8080
site2.domain.example maps to domain.example:8081

Because often you want to fully utilize the server space you have bought. Then you can try the following:

Step 1: Install proxy server. I will use Nginx here.

apt-get install nginx

Step 2: Edit /etc/nginx/nginx.conf file to add the port mappings. To do so, add the following lines:

server {
    listen 80;
    server_name site1.domain.example;

    location / {
        proxy_pass http://localhost:8080;
    }
}

server {
    listen 80;
    server_name site2.domain.example;

    location / {
        proxy_pass http://localhost:8081;
    }
}

This does the magic. So the file will end up looking like following:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##
server {
    listen 80;
    server_name site1.domain.example;

    location / {
        proxy_pass http://localhost:8080;
    }
}

server {
    listen 80;
    server_name site2.domain.example;

    location / {
        proxy_pass http://localhost:8081;
    }
}

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

Step 3: Start Nginx:

/etc/init.d/nginx start.

Whenever you make any changes to configuration, you need to restart Nginx:

/etc/init.d/nginx restart

Finally: Don't forget to add A records in your DNS configuration. All subdomains should point to domain. Like this:

Put your static IP instead of 111.11.111.111

Further details:

Host Static Website: If you have any static website (Like angular app), that you want to deploy in Nginx itself. Place your index.html along with the other resources in some folder, like /srv/mySite and add the following server block in nginx.conf:

server {
    listen 80;
    server_name staticSite.domain.example;
    root /srv/mySite;
    location / {
       try_files $uri $uri/ /index.html;
    }
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Deb
  • 5,163
  • 7
  • 30
  • 45
  • This worked great for me! FWIW, I needed this reverse-proxy arrangement for my development environment. So instead of fiddling with my A records, I simply added records to my local /etc/hosts file; this is easier and less risky and than changing my DNS records in my nameservers. – Gurjeet Singh Jan 16 '21 at 09:13
  • possible to do this in apache? – Nikhil VJ Dec 15 '22 at 19:09
  • Note: The `server` setting block must exist in the `http` block, otherwise it will fail the syntax test (`nginx -t`) and the service will not start. – LianSheng Apr 04 '23 at 13:21
6

If you only got one IP on the server, there is no chance to do that. DNS is a simple name to number (IP) resolver. If you have two IPs on the server, you can point each subdomain to each of the IP-addresses and run both servers on the default port on each IP.
one.example.com -> 127.0.0.1 (server: 127.0.0.1:25565)
two.example.com -> 127.0.0.2 (server: 127.0.0.2:25565)

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
unNamed
  • 969
  • 1
  • 10
  • 18
3

If you wish to use 2 subdomains to other ports, you can use Minecraft's proxy server (it means BungeeCord, Waterfall, Travertine...), and bind subdomain to specifiend in config.yml server. To do that you have to setup your servers in BungeeCord's config:

servers:
  pvp:
    motd: 'A Minecraft Server PVP'
    address: localhost:25566
    restricted: false
  skyblock:
    motd: 'A Minecraft Server SkyBlock'
    address: localhost:25567
    restricted: false

Remember! Ports must be diffrent than default Minecraft's port (it means 25565), because we will use this port to our proxy. sub1.domain.example and sub2.domain.example we have to bind to server where you have these servers. Now, we have to bind subdomains in your Bungee server:

listeners:
    forced_hosts:
      sub1.domain.example: pvp
      sub2.domain.example: skyblock
      domain.example: pvp // You can bind other domains to same servers.

Remember to change force_default_server to true, and change host to 0.0.0.0:25565 Example of BungeeCord's config.yml with some servers: https://pastebin.com/tA9ktZ6f Now you can connect to your pvp server on sub1.domain.example and connect to skyblock on sub2.domain.example. Don't worry, BungeeCord takes only 0,5GB of RAM for 500 players.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Paisley
  • 31
  • 2
3

I... don't think so. You can redirect the subdomain (such as blah.something.example) to point to something.example:25566, but I don't think you can actually set up the subdomain to be on a different port like that. I could be wrong, but it'd probably be easier to use a simple .htaccess or something to check %{HTTP_HOST} and redirect according to the subdomain.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

With only 1 IP you can forget DNS but you can use a MineProxy because the handshake packet of the client contains the host that then he connected to and a MineProxy will ready this host and proxy the connection to a server that is registered for that host

Lost2
  • 138
  • 1
  • 13