16

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?

3
  • 6
    No. The hosts file doesn't have anything to do with ports. – DavidPostill Mar 27 '17 at 13:01
  • 3
    What 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. – Giacomo1968 Mar 27 '17 at 13:29
  • 4
    Isn't this a duplicate of a dozen questions already – user1686 Mar 27 '17 at 13:51
13

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.

7

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

3

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

2

The solution that worked for me using apache was first to modify my apache config like this:

Web Server Config

<VirtualHost *:80>
    ServerName testdev.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/testdev.com/public

    <Directory /var/www/testdev.com/public>
        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

And dont forget to restart apache after you've modified any config file. Notice the ServerName Directive has been set to the domain name you want to use, and the VirtualHost *:80 is set to 80. This will direct all requests from your browser that hit your domain name on port 80 to this virtual host configuration.

Now you have to modify your hosts file as below:

Hosts File

127.0.0.1   localhost
127.0.0.1   testdev.com

Now head to your browser and type in testdev.com this will direct you to your web app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.