Can I map a IP address and a port with /etc/hosts?

6

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

For example, I’d like to map 127.0.0.1:8000 to testdev.com

My /etc/hosts file has

127.0.0.1  localhost
127.0.0.1:8000 testdev.com

So that when I hit testdev.com, it refers 127.0.0.1:8000. I tried the above one, but it doesn’t work. Any other alternative way to achieve this?

Tamil

Posted 2017-03-27T13:00:49.933

Reputation: 163

5No. The hosts file doesn't have anything to do with ports. – DavidPostill – 2017-03-27T13:01:50.110

3What David Postill says is correct, but if you are really hoping to get rid of the 8000 port for local testing you might want to investigate setting up a reverse proxy via Apache or Nginx. That is the most common and accepted way to map a port-based address like 127.0.0.1:8000 to testdev.com. – JakeGould – 2017-03-27T13:29:54.007

4Isn't this a duplicate of a dozen questions already – user1686 – 2017-03-27T13:51:14.640

Answers

3

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.

Some applications like Minecraft support checking a DNS server's SRV record and can use a port number from that, but again, this is dependent on the program's behavior and can't be done from your /etc/hosts file.


I’d like to map 127.0.0.1:8000 to testdev.com

What @JakeGould in the comments says is what you need to do.

LawrenceC

Posted 2017-03-27T13:00:49.933

Reputation: 63 487

2

This solution worked for me with nginx
If you are using nginx you can use Nginx as reverse proxy and do something like this below

Example
server { listen testdev.com:80; server_name testdev.com; location / { proxy_pass http://127.0.0.1:8000; } }
For more information regarding the above method check this here
For window users
I found this solution from stackoverflow here

Charles

Posted 2017-03-27T13:00:49.933

Reputation: 121

1

As Lawrence has already said it is not possible through /etc/hosts but you can set a reverse proxy in order to achieve it using nginx or apache. I had the same problem in the past so I made this tool to achieve this with a /etc/hosts syntax: https://github.com/cristianoliveira/ergo

Cristian Oliveira

Posted 2017-03-27T13:00:49.933

Reputation: 111