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.comSo 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?
4 Answers
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.
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 }
}
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:
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/ <Directory /var/www/ 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 noetAnd 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.comNow head to your browser and type in testdev.com this will direct you to your web app