4

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?
4
  • 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?)
    – TooTea
    Oct 19 at 9:35
  • 1
    If you decide to proxy following the other answers, give a try to Caddy (caddyserver.com) which makes this very easy.
    – WoJ
    Oct 19 at 10:33
  • You can try adding a custom protocol handler. It might not work that well for your use-case, but it's something you can easily try out in a couple of minutes.
    – Aaron F
    Oct 19 at 18:53
  • The concept of well-known ports is important here. Oct 20 at 1:54
7

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

5
  • 1
    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)
    – WoJ
    Oct 19 at 10:31
  • 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. Oct 19 at 10:34
  • 3
    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.
    – WoJ
    Oct 19 at 10:50
  • 1
    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.
    – Peeja
    Oct 19 at 18:11
  • 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. Oct 19 at 19:53
4

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.
2

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!!

2
  • 1
    I already have IP aliases using etc/hosts so I was hoping for the same solution for ports as well :) Thanks! Oct 19 at 7:07
  • 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. Oct 19 at 7:40
0

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.

4
  • Persumably he already has something else running on port 80, or he wouldn't need to use 8080 for this.
    – Barmar
    Oct 19 at 14:30
  • 1
    @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. Oct 19 at 15:10
  • 2
    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.
    – Barmar
    Oct 19 at 15:12
  • @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.) Oct 21 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.