在 Windows 主机文件中使用端口号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8652948/
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
Using port number in Windows host file
提问by Charles Yeung
After installing TeamViewer, I have changed the wampserver port to 8080, so the address is http://localhost:8080.
安装TeamViewer后,我把wampserver的端口改成了8080,所以地址是 http://localhost:8080.
For the host file located at C:\WINDOWS\system32\drivers\etc\, I have also made the change as below
对于位于 C:\WINDOWS\system32\drivers\etc\ 的主机文件,我也进行了如下更改
BEFORE127.0.0.1 www.example.com
前127.0.0.1 www.example.com
AFTER127.0.0.1:8080 www.example.com
后127.0.0.1:8080 www.example.com
When I access www.example.com, it doesn't redirect to my wampserver, how can I fix it?
当我访问 www.example.com 时,它没有重定向到我的 wampserver,我该如何解决?
回答by Mat
The hosts
file is for host name resolution only (on Windows as well as on Unix-like systems). You cannot put port numbers in there, and there is no way to do what you want with generic OS-level configuration - the browser is what selects the port to choose.
该hosts
文件仅用于主机名解析(在 Windows 以及类 Unix 系统上)。你不能把端口号放在那里,而且没有办法用通用的操作系统级配置来做你想做的 - 浏览器是选择要选择的端口的东西。
So use bookmarks or something like that.
(Some firewall/routing software might allow outbound port redirection, but that doesn't really sound like an appealing option for this.)
所以使用书签或类似的东西。
(某些防火墙/路由软件可能允许出站端口重定向,但这听起来并不是一个吸引人的选项。)
回答by Rajat Gupta
What you want can be achieved by modifying the hosts file through Fiddler 2application.
您可以通过Fiddler 2应用程序修改主机文件来实现您想要的。
Follow these steps:
按着这些次序:
- Install Fiddler2
- Navigate to Fiddler2 menu:- Tools> HOSTS.. (Click to select)
Add a line like this:-
localhost:8080 www.mydomainname.com
- Save the file & then checkout
www.mydomainname.com
in browser.
- 安装Fiddler2
- 导航到 Fiddler2 菜单:- Tools> HOSTS..(点击选择)
添加这样的一行:-
localhost:8080 www.mydomainname.com
- 保存文件,然后
www.mydomainname.com
在浏览器中结帐。
回答by Pau Coma Ramirez
I managed to achieve this by using Windows includedNetworking tool netsh
.
我设法通过使用Windows 包含的网络工具来实现这一点netsh
。
As Mat points out : The hosts file is for host name resolution only, so a combination of the two did the trick for me.
正如 Mat 指出的那样:hosts 文件仅用于主机名解析,因此两者的结合对我来说很有效。
Example
例子
Overview
概述
example.app:80
| <--Link by Hosts File
+--> 127.65.43.21:80
| <--Link by netsh Utility
+--> localhost:8081
Actions
行动
- Started my server on
localhost:8081
- Added my "local DNS" in the hosts fileas a new line
127.65.43.21 example.app
- Any free address in the network
127.0.0.0/8
(127.x.x.x
) can be used. - Note:I am assuming
127.65.43.21:80
is not occupied by another service. - You can check with
netstat -a -n -p TCP | grep "LISTENING"
- Any free address in the network
- added the following network configuration with netsh commandutility
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
- I can now access the server at
http://example.app
- 启动我的服务器
localhost:8081
- 在主机文件中添加了我的“本地 DNS”作为新行
127.65.43.21 example.app
- 可以使用网络
127.0.0.0/8
(127.x.x.x
)中的任何空闲地址。 - 注意:我假设
127.65.43.21:80
没有被其他服务占用。 - 你可以检查
netstat -a -n -p TCP | grep "LISTENING"
- 可以使用网络
- 使用netsh 命令实用程序
添加了以下网络配置
netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
- 我现在可以访问服务器
http://example.app
Notes:
- These commands/file modifications need to be executed with Admin rights
注意:
- 这些命令/文件修改需要以管理员权限执行
- netsh portproxy needs ipv6 librarieseven only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: netsh interface ipv6 install
- netsh portproxy 需要 ipv6 库,即使只使用 v4tov4,通常它们也会被默认包含,否则使用以下命令安装它们:netsh interface ipv6 install
You can see the entryyou have added with the command:
你可以看到你用命令添加的条目:
netsh interface portproxy show v4tov4
netsh interface portproxy show v4tov4
You can remove the entrywith the following command:
您可以使用以下命令删除条目:
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21
netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21
Links to Resources:
资源链接:
回答by Sergey
Fiddler2 -> Rules -> Custom Rules
Fiddler2 -> 规则 -> 自定义规则
then find function OnBeforeRequest on put in the next script at the end:
然后在最后的下一个脚本中找到 OnBeforeRequest 函数:
if (oSession.HostnameIs("mysite.com")){
oSession.host="localhost:39901";
}
回答by Belal Mohammed
-You can use any free address in the network 127.0.0.0/8 , in my case needed this for python flask and this is what I have done : add this line in the hosts file (you can find it is windows under : C:\Windows\System32\drivers\etc ) :
-您可以使用网络 127.0.0.0/8 中的任何免费地址,在我的情况下,python flask 需要这个地址,这就是我所做的:在主机文件中添加这一行(您可以在以下位置找到它:C: \Windows\System32\drivers\etc ):
127.0.0.5 flask.dev
Make sure the port is the default port "80" in my case this is what in the python flask:
app.run("127.0.0.5","80")
now run your code and browse
flask.dev
在我的情况下,确保端口是默认端口“80”,这就是 python 烧瓶中的端口:
app.run("127.0.0.5","80")
现在运行您的代码并浏览
flask.dev
回答by Cristian Oliveira
The simplest way is using Ergo as your reverse proxy: https://github.com/cristianoliveira/ergo
最简单的方法是使用 Ergo 作为您的反向代理:https: //github.com/cristianoliveira/ergo
You set your services and its IP:PORT and ergo routes it for you :).
您设置您的服务及其 IP:PORT 并为您路由它:)。
You can achieve the same using nginx
or apache
but you will need to configure them.
您可以使用nginx
或实现相同的功能,apache
但您需要配置它们。
回答by aards
Using netsh with connectaddress=127.0.0.1 did not work for me.
使用带有 connectaddress=127.0.0.1 的 netsh 对我不起作用。
Despite looking everywhere on the internet I could not find the solution which solved this for me, which was to use connectaddress=127.x.x.x (i.e. any 127. ipv4 address, just not 127.0.0.1) as this appears to link back to localhost just the same but without the restriction, so that the loopback works in netsh.
尽管在互联网上到处寻找,但我找不到为我解决这个问题的解决方案,即使用 connectaddress=127.xxx(即任何 127.ipv4 地址,而不是 127.0.0.1),因为这似乎链接回本地主机相同但没有限制,以便环回在 netsh 中工作。
回答by DogBot
If what is happening is that you have another server running on localhost and you want to give this new server a different local hostname like
http://teamviewer/
如果发生的情况是您有另一台服务器在 localhost 上运行,并且您想为这个新服务器提供一个不同的本地主机名,例如
http://teamviewer/
I think that what you are actually looking for is Virtual Hosts functionality. I use Apache so I do not know how other web daemons support this. Maybe it is called Alias. Here is the Apache documentation:
我认为您实际上正在寻找的是虚拟主机功能。我使用 Apache,所以我不知道其他 Web 守护程序如何支持这一点。也许它被称为别名。这是 Apache 文档:
回答by Musa
You need NGNIX or Apache HTTP server as a proxy server for forwarding http requests to appropriate application -> which listens particular port (or do it with CNAME which provides Hosting company). It is most powerful solution and this is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server.
您需要 NGNIX 或 Apache HTTP 服务器作为代理服务器,将 http 请求转发到适当的应用程序 -> 侦听特定端口(或使用提供托管公司的 CNAME 进行)。这是最强大的解决方案,这只是一种非常简单的方法来继续添加新的子域,或者在 DNS 记录指向服务器时自动添加新域。
Apache era call it Virtual host -> httpd.apache.org/docs/trunk/vhosts/examples.html
NGINX -> Server Block https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/
Apache 时代称之为虚拟主机 -> httpd.apache.org/docs/trunk/vhosts/examples.html
NGINX -> 服务器块 https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/