Can I map a hostname *and* a port with /etc/hosts? [closed]
Asked Answered
A

2

226

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

Adultery answered 23/5, 2012 at 23:0 Comment(2)
You cannot map the port number in /etc/hosts. Instead you can define as 127.0.0.1 api.mydomain.com and access it in the browser like api.mydomain.com:8080Witkowski
I read somewhere that dns supports this. You can have a record that says on www.example.com http is on port 80 and also an A record to say that the address is 127.0.0.1 it also said it was not well supported. It did not say much after that.Alessi
H
229

No, that's not possible. The port is not part of the hostname, so it has no meaning in the hosts-file.

Homopterous answered 23/5, 2012 at 23:3 Comment(0)
A
172

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;
  }
}
Adventist answered 23/5, 2012 at 23:5 Comment(5)
I think the line of proxy_pass should look like this: proxy_pass http://127.0.0.1:8000; I got "invalid URL prefix" on nginx 1.4.3.Horsefaced
thanks for this wonderful tip! simple solution for most servers.Dusen
I am using this trick to map ports of a remote machine (e.g. cloud.app:80 while the actual port is 8080). Very useful for testing Confluence nodes of a cluster but accessing them on the same base URL. Thanks!Puga
I had basically the same problem and the solution using this reverse-proxy is really helpful. Although, I wanted some simpler solution so I made this tool: github.com/cristianoliveira/ergo I hope it help in somehow :)Gone
Thanks @CristianOliveira ! This helped me a LOT :)Hierarch

© 2022 - 2024 — McMap. All rights reserved.