Linux 我可以使用 /etc/hosts 映射主机名*和*端口吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10729034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:28:10  来源:igfitidea点击:

Can I map a hostname *and* a port with /etc/hosts?

linuxdnsporthostname

提问by Carson

Can I map an IP address like 127.0.0.1to a domain name anda port?

我可以将 IP 地址映射127.0.0.1到域名端口吗?

For example, I would like to map 127.0.0.1to api.example.com:8000

例如,我想映射127.0.0.1api.example.com:8000

采纳答案by mata

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

不,那不可能。端口不是主机名的一部分,因此它在hosts-file 中没有意义。

回答by Eric Fortis

If you really need to do this, use reverse proxy.

如果您确实需要这样做,请使用反向代理。

For example, with nginx as reverse proxy

比如用nginx做反向代理

server {
  listen       api.mydomain.com:80;
  server_name  api.mydomain.com;
  location / {
    proxy_pass http://127.0.0.1:8000;
  }
}