Score:4

is it possible to alias a port number?

us flag

I want to run a service on a custom port (e.g. jenkins on 8080) and I prefer to access it via browser with a name I can remember, e.g. http://localhost:jenkins which will be an alias for http://localhost:8080

  1. Is such a thing possible?
  2. If so, will it be only an HTTP thing, or all network protocols will know of this alias?
  3. Also, if (1) is true, is this feature supported on other OS (centos, windows, etc.)

Edit:

  1. What are the moving parts from the moment I type in address until it gets resolved? I'm guessing that the browser parsers, and therefore there might be a browser that supports my request. After that, the parsed url is probably passed to an OS process which in turn can also implement my request and accept str instead of an int... Are any of these parts configurable in a such a way that it will answer my need?
TooTea avatar
cn flag
Where do you need this to work? Just on a handful of machines under your control? (Is "localhost" just an example, or is this really meant to apply only to one machine?)
cn flag
WoJ
If you decide to proxy following the other answers, give a try to Caddy (https://caddyserver.com/) which makes this very easy.
Aaron F avatar
cn flag
You can try [adding a custom protocol handler](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/registerProtocolHandler#example). It might not work that well for your use-case, but it's something you can easily try out in a couple of minutes.
chrylis -cautiouslyoptimistic- avatar
The concept of _well-known ports_ is important here.
Score:7
vn flag

This can be solved with "reverse proxying" - so research into this field a bit.

It can't do what you're actually asking (alias a port number), but it can give you another way to make an easy path for each service (by forwarding DNS records to a specific IP and port).

Basically, what you can do with a reverse proxy is:

  • Ensure all your sites present an SSL certificate, and (if you choose) enforce HTTPS in this case
  • Map http://localhost:8080 to https://jenkins.local (port forward)
  • Map http://localhost/jenkins to https://jenkins.local (location forward)

The above works with both internal and external DNS resolving

I'm personally using Nginx Proxy Manager, but Traefik is also a popular choice.

To get an idea what to setup, Nginx Proxy Manager looks like this:

enter image description here

cn flag
WoJ
Using HTTPS is not a prerequisite to reverse proxying. It is probably a good idea, though (for a variety of reasons, security being one of them - but YMMV)
Artur Meinild avatar
vn flag
Agreed - but I don't believe I ever said it was a prerequisite - I gave it as an example of what you can do. However, I really can't see any reason in 2021 not to use HTTPS when possible, so you could say I'm assuming that most people want to use it.
cn flag
WoJ
Well, your first point states "ensure" and "enforce". In any case, the decision to use or not HTTPS is up to the administrator, I just wanted to highlight that this is not something you have to do. There are plenty of cases where it is easier (and still safe) to use HTTP.
mx flag
Just to be clear, since it's non-obvious without reading the other answers: this doesn't actually do *quite* what the asker asked (which was to use a symbolic name for a port number), but that's not actually possible, and this is the *closest* thing that works and solves the underlying problem.
Artur Meinild avatar
vn flag
I agree. I was trying to give a solution to the underlying problem, as the OP states: "... and I prefer to access it via browser with a name I can remember". I edited my answer to reflect this.
Score:4
ng flag

From rfc 1738 https://datatracker.ietf.org/doc/html/rfc1738#section-3.1

port in the url is a number

port

    The port number to connect to. Most schemes designate
    protocols that have a default port number. Another port number
    may optionally be supplied, in decimal, separated from the
    host by a colon. If the port is omitted, the colon is as well.
Score:3
us flag

It is possible to use solutions such as Nginx, Apache, squid, lighthttpd, etc. Do note that these will map your port to whatever name you give but it will be something independent from the internal localhost. You can use this software in Linux and window based systems.

Here's an apache example.

Listen IP_ADDR:80
NameVirtualHost IP_ADDR:80

<VirtualHost IP_ADDR:80>
  ServerName  yourname.yourdomain

  ProxyPass        / http://localhost:10000/
  ProxyPassReverse / http://localhost:10000/

</VirtualHost>

Another solution, if you wish to keep the servers memorable, is to give Jenkins a different IP all together by running it on a different server and then mapping it in /etc/hosts as 192.168.1.1 Jenkins (You basically add this in the file). After doing this let us say your Jenkins is running on 192.168.1.1:3000 you can access it by http://jenkins:3000.

Even if you do this, all network protocols won't work, you won't be able to access Jenkins using https://jenkins:3000.

Hope this helps!!

CIsForCookies avatar
us flag
I already have IP aliases using `etc/hosts` so I was hoping for the same solution for ports as well :) Thanks!
Guntram Blohm avatar
cn flag
There is /etc/services, but browsers generally don't use it, and even if they did, you'd need to implement on the client, not the server.
Score:0
na flag

Probably the simplest solution is to use redir to redirect the ports, e.g. run redir :80 127.0.0.1:8080, or even better put it into your inetd.conf. If you do not like the traffic going wastefully through another process, the same can be achieved using iptables forwarding (but that is more hassle).

True way of creating an alias would be to use a SRV record in your DNS server and that would work well with no overhead, but because of history, SRV records are ignored for the http/https protocol.

cn flag
Persumably he already has something else running on port 80, or he wouldn't need to use 8080 for this.
Radovan Garabík avatar
na flag
@Barmar who knows... I guess more likely it is some kind of a web framework that he runs under a non-root account. But even so, this setup is still possible if he has multiple interfaces (with separate ip addresses) available.
cn flag
you're not really answering the question, you're saying that they don't need to use port 8080 in the first place. The question is just about using a name instead of number for the port.
Radovan Garabík avatar
na flag
@Barmar you are right... I mis-read the colon as a dot. In that case, I am afraid there is no server solution to the OP's requirement (no reverse proxy etc.)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.