Can I map a hostname *and* a port with /etc/hosts?

LinuxDnsPortHostname

Linux Problem Overview


Can I map an IP address like 127.0.0.1 to a domain name and a port?

For example, I would like to map 127.0.0.1 to api.example.com:8000

Linux Solutions


Solution 1 - Linux

No, that's not possible. The port is not part of the hostname, so it has no meaning in the hosts-file.

Solution 2 - Linux

If you really need to do this, use a reverse proxy. For example, with Nginx:

server {
  listen       api.mydomain.com:80;
  server_name  api.mydomain.com;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarsonView Question on Stackoverflow
Solution 1 - LinuxmataView Answer on Stackoverflow
Solution 2 - LinuxEric FortisView Answer on Stackoverflow