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

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

My /etc/hosts file has  localhost testdev.com

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

  • 6
    No. The hosts file doesn't have anything to do with ports.
    – DavidPostill
    Mar 27, 2017 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 to testdev.com. Mar 27, 2017 at 13:29
  • 5
    Isn't this a duplicate of a dozen questions already
    – user1686
    Mar 27, 2017 at 13:51

4 Answers 4


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


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 to testdev.com

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


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


server {
  listen       testdev.com:80;
  server_name  testdev.com;
  location / {

For more information regarding the above method check this here
For window users
I found this solution from stackoverflow here


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


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

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

# 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   localhost   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.