Do port numbers "matter" in etc/hosts?

This might be another of my stupid questions … but in my etc/hosts file there is the following.

# Standard host addresses
127.0.0.1  localhost
::1        localhost ip6-localhost ip6-loopback
ff02::1    ip6-allnodes
ff02::2    ip6-allrouters
# This host address
127.0.1.1  gary-hpelitedesk800g3sff

I’m working on an application that starts a server socket in Tcl and programmatically opens a browser instructing it to open to a specific url—for the moment, that is 127.0.0.15 with a Tcl generated random unused port number. It’s generated by the command that starts the server socket. I don’t have to use that option and could just provide a port number but like that this should prevent conflicts with other applications trying to use the same port.

I’m trying to use a service worker in the browser and when it is registered it is identified with the port number, such that I cannot query all the existing service workers for 127.0.0.15 when it loads. For example, if it open with 127.0.0.15:33789, a service worker is registered for that scope. Then when it loads again with 127.0.0.15:42001 a query will be empty because I can’t “see” 127.0.0.15:33789 because it’s considered a different scope.

I’d like to test what will occur if 127.0.0.15 is given a string name, like “appname”. Will the browser register the service workers under appname or appname:port?

The question, finally, is can I just add a line to etc/hosts of “127.0.0.15 appname” or is that always done with a port like (perhaps the ::1 on line 2)? Will appname be associated with every 127.0.0.15:random_port or for one port only? That’s regardless of what the browser does with it, I’d just like to know how to propery add it to etc/hosts, so I can test it properly.

Thank you.

Yes.

Those are not ports. Those are shorthand notations for IPv6 addresses. :wink:

You seem to believe that “appname” would be the name of a process. :face_with_raised_eyebrow:

Well, it’s not. It’s a locally recognized hostname associated with that particular (local) IP address, and therefore, “appname” will refer to said IP address, not to any particular port number on said IP interface.

Thank you.

My idea does not solve my problem, though.

I start the server with one of the following.
lassign [StartServer 127.0.0.15 0] addr host port – generate random port
lassign [StartServer 127.0.0.15 34859] addr host port – uses port 34859
lassign [StartServer appname 34859] addr host port – uses port 34859
I’d like to have this work
lassign [StartServer appname 0] addr host port – random port

But when lassign [StartServer appname 34859] addr host port opens the browser, the url in the browser is http://127.0.0.15:34859/login and the service worker scope still depends on the port. I was expecting it to be http://appname/login and have the service worker scope be http://appname for all ports when random.

I guess there’s just no way around that.

It’s been a few years but I thought I recalled doing this when using LAMP and I could type www.appname in the browser’s location box and it would load my local file and show that as the url.

@Aragorn I think I remember now, I had to do something with <VirtualHost> in Apache. Probably nothing like that in regular Tcl. I just won’t use a random port.

Another reason to not use a random port is that the port goes into the url that is used as a key in the cache API used by the service worker, which means any data cached during one session won’t be reachable by the next session if the port is different. Probably common knowledge but new stuff for me.

It’s a bit tricky to set things up that way, but not impossible. See the following Wikipedia article… :backhand_index_pointing_down:


Also perhaps worth noting, after modifying /etc/hosts you should run… :backhand_index_pointing_down:

sudo systemctl daemon-reload

… or reboot, because I don’t think systemd will pick up the changes without it. :thinking:

Processes using ports below 1024 require elevated privileges, so we let it bind to a higher port and redirect it elsewhere.

If you use the same port each time, you can just forward connections there.

The Apache <VirtualHost> is somewhat different. Apache can use the hostname from the header to decide which virtual host handles the request. Tcl cannot do anything like that out-of-box.

Myself, I would just setup port forwarding with the kernel’s firewall.

e.g.

sudo nft add table ip nat

table ip nat {
  chain output {
    type nat hook output priority -100;
    tcp dport 80 redirect to :34859
  }
}

That will forward TCP port 80 connections to 34859.

So you can use http://localhost/login, or map the host in /etc/hosts.

If this is what you need and it works, it can easily be put into a config file to persist.