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
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.
/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.
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.
No, that's not possible. The port is not part of the hostname, so it has no meaning in the hosts
-file.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With