Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 219
Carson Avatar asked Oct 03 '22 17:10

Carson


People also ask

Can I map a IP address and a port with ETC hosts?

No. The /etc/hosts file is part of your system's domain name resolver (it will check this file, then check DNS). The resolver's job is to convert text domain names to an IP address, not an IP address + port.

Can you specify a port in etc hosts?

/etc/hosts won't let you specify a port, but a combination of aliasing 127.0. 0.1 to 127.0. 0. X, forwarding ports from 8000 to 80, and adding the 127.0.

Is port part of hostname?

A host name is only the name of a server, which should be resolvable to one or more IP addresses. Ports have nothing to do with host names at all.


2 Answers

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

like image 74
mata Avatar answered Oct 13 '22 12:10

mata


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;
  }
}
like image 25
Eric Fortis Avatar answered Oct 13 '22 12:10

Eric Fortis