Coder Perfect

Is it possible to use /etc/hosts to map both a hostname and a port? [closed]

Problem

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

Asked by Carson

Solution #1

No, that’s not going to work. The port has no meaning in the hosts-file because it is not part of the hostname.

Answered by mata

Solution #2

Use a reverse proxy if you truly need to accomplish this. For instance, consider Nginx:

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

Answered by Eric Fortis

Post is based on https://stackoverflow.com/questions/10729034/can-i-map-a-hostname-and-a-port-with-etc-hosts