Windows 主机文件中的通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/138162/
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
Wildcards in a Windows hosts file
提问by EvilPuppetMaster
I want to setup my local development machine so that any requests for *.local
are redirected to localhost
. The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local
, site2.local
etc, and have them all resolve to localhost
, while Apache serves a different site accordingly.
我想设置我的本地开发机器,以便将任何请求*.local
重定向到localhost
. 这个想法是,我开发多个网站,我可以再补充虚拟主机到Apache叫site1.local
,site2.local
等,让他们都决心localhost
,而Apache的相应服务于不同的网站。
I am on Windows XP.
我在 Windows XP 上。
I tried adding
我尝试添加
127.0.0.1 *.local
to my c:\windows\system32\drivers\etc\hosts
file, also tried:
到我的c:\windows\system32\drivers\etc\hosts
文件,也试过:
127.0.0.1 .local
Neither of which seem to work.
这两个似乎都不起作用。
I know I can set them up on different port numbers, but that is a pain since it is hard to remember which port is which.
我知道我可以在不同的端口号上设置它们,但这很痛苦,因为很难记住哪个端口是哪个。
I don't want to have to setup a local DNS server or anything hard, any suggestions?
我不想设置本地 DNS 服务器或任何困难,有什么建议吗?
回答by Petah
Acrylic DNS Proxy (free, open source) does the job. It creates a proxy DNS server (on your own computer) with its own hosts file. The hosts file accepts wildcards.
丙烯酸 DNS 代理(免费,开源)可以完成这项工作。它使用自己的主机文件创建代理 DNS 服务器(在您自己的计算机上)。主机文件接受通配符。
Download from the offical website
从官网下载
http://mayakron.altervista.org/support/browse.php?path=Acrylic&name=Home
http://mayakron.altervista.org/support/browse.php?path=Acrylic&name=Home
Configuring Acrylic DNS Proxy
配置亚克力 DNS 代理
To configure Acrylic DNS Proxy, install it from the above link then go to:
要配置 Acrylic DNS 代理,请从上面的链接安装它,然后转到:
- Start
- Programs
- Acrylic DNS Proxy
- Config
- Edit Custom Hosts File (AcrylicHosts.txt)
- 开始
- 程式
- 亚克力 DNS 代理
- 配置
- 编辑自定义主机文件 (AcrylicHosts.txt)
Add the folowing lines on the end of the file:
在文件末尾添加以下几行:
127.0.0.1 *.localhost
127.0.0.1 *.local
127.0.0.1 *.lc
Restart the Acrylic DNS Proxy service:
重启 Acrylic DNS 代理服务:
- Start
- Programs
- Acrilic DNS Proxy
- Config
- Restart Acrylic Service
- 开始
- 程式
- Acrilic DNS 代理
- 配置
- 重启亚克力服务
You will also need to adjust your DNS setting in you network interface settings:
您还需要在网络接口设置中调整 DNS 设置:
- Start
- Control Panel
- Network and Internet
- Network Connections
- Local Area Connection Properties
- TCP/IPv4
- 开始
- 控制面板
- 网络和互联网
- 网络连接
- 本地连接属性
- TCP/IPv4
Set "Use the following DNS server address":
设置“使用以下 DNS 服务器地址”:
Preferred DNS Server: 127.0.0.1
If you then combine this answer with jeremyasnyder's answer (using VirtualDocumentRoot
) you can then automatically setup domains/virtual hosts by simply creating a directory.
如果您随后将此答案与 jeremyasnyder 的答案(使用VirtualDocumentRoot
)结合起来,您就可以通过简单地创建目录来自动设置域/虚拟主机。
回答by jeremyasnyder
To answer your question, you cannot use wildcards in the hosts file under Windows.
回答您的问题,您不能在Windows 下的hosts 文件中使用通配符。
However, if you want to only change the hosts file to make new sites work.... you can configure your Apache like this and you don't have to keep editing it's config:
但是,如果您只想更改主机文件以使新站点正常工作....您可以像这样配置您的 Apache,而不必继续编辑它的配置:
http://postpostmodern.com/instructional/a-smarter-mamp/
http://postpostmodern.com/instructional/a-smarter-mamp/
Basically a quick summary based on my setup, add the following to your apache.conf file:
基本上是基于我的设置的快速摘要,将以下内容添加到您的 apache.conf 文件中:
LoadModule vhost_alias_module modules/mod_vhost_alias.so
NameVirtualHost *:80
<Directory "/xampp/sites">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
VirtualDocumentRoot c:/xampp/sites/%-1/%-2+/
</VirtualHost>
This allows me to add an entry like:
这允许我添加一个条目,如:
127.0.0.1 test.dev
and then make the directory, c:\xampp\sites\dev\test and place the necessary files in there and it just works.
然后创建目录 c:\xampp\sites\dev\test 并将必要的文件放在那里,它就可以工作了。
The other option is to use <Directory>
tags in apache.conf and reference the pages from http://localhost/project/.
另一种选择是<Directory>
在 apache.conf 中使用标签并从http://localhost/project/引用页面。
回答by Biri
I don't think that it is possible.
我不认为这是可能的。
You anyway have to modify the apache virtualroot entries every time you add a new site and location, so it's not a big work to syncronise the new name to the Windows vhost file.
无论如何,每次添加新站点和位置时都必须修改 apache virtualroot 条目,因此将新名称同步到 Windows vhost 文件并不是一项大工作。
Update: please check the next answer and the comments on this answer. This answer is 6 years old and not correct anymore.
更新:请检查下一个答案和对此答案的评论。这个答案已有 6 年历史,不再正确。
回答by Simon East
To add to the great suggestions already here, XIP.IOis a fantastic wildcard DNS server that's publicly available.
除了这里已经有的好建议之外,XIP.IO是一个出色的通配符 DNS 服务器,它是公开可用的。
myproject.127.0.0.1.xip.io -- resolves to --> 127.0.0.1
other.project.127.0.0.1.xip.io -- resolves to --> 127.0.0.1
other.machine.10.0.0.1.xip.io -- resolves to --> 10.0.0.1
(The ability to specify non-loopback addresses is fantastic for testing sites on iOS devices where you cannot access a hosts file.)
(指定非环回地址的能力对于在无法访问主机文件的 iOS 设备上测试站点非常有用。)
If you combine this with some of the Apache configuration mentioned in other answers, you can potentially add VirtualHosts with zero setup.
如果您将其与其他答案中提到的某些 Apache 配置相结合,您可能会添加 VirtualHosts零设置。
回答by Kevin Hakanson
I found a posting about Using the Windows Hosts Filethat also says "No wildcards are allowed."
我发现一篇关于使用 Windows 主机文件的帖子也说“不允许使用通配符”。
In the past, I have just added the additional entries to the hosts file, because (as previously said), it's not that much extra work when you already are editing the apache config file.
过去,我刚刚在 hosts 文件中添加了额外的条目,因为(如前所述),当您已经在编辑 apache 配置文件时,并没有那么多额外的工作。
回答by Joe
Editing the hosts file is less of a pain when you run "ipconfig /flushdns" from the windows command prompt, instead of restarting your computer.
当您从 Windows 命令提示符运行“ipconfig /flushdns”而不是重新启动计算机时,编辑主机文件就不那么痛苦了。
回答by Stu Thompson
You could talk your network administrator into setting up a domain for you (say 'evilpuppetmaster.hell') and having the wildcard there so that everything (*.evilpuppetmaster.hell') resolves to your IP
您可以让您的网络管理员为您设置一个域(例如“evilpuppetmaster.hell”)并在那里使用通配符,以便所有内容(*.evilpuppetmaster.hell')都解析为您的 IP
回答by Matthew Skelton
We have this working using wildcard DNS in our local DNS server: add an A
record something like *.local -> 127.0.0.1
我们在本地 DNS 服务器中使用通配符 DNS 进行此工作:添加A
类似的记录*.local -> 127.0.0.1
I think that your network settings will need to have the chosen domain suffix in the domain suffix search list for machines on the network, so you might want to replace .local
with your company's internal domain (e.g. .int
) and then add a subdomain like .localhost.int
to make it clear what it's for.
我认为您的网络设置将需要在网络上机器的域后缀搜索列表中选择域后缀,因此您可能希望替换.local
为您公司的内部域(例如.int
),然后添加子域之类的.localhost.int
以使其清楚这是为了什么。
So *.localhost.int
would resolve to 127.0.0.1
for everybody on the network, and config file settings for all developers would "just work" if endpoints hang off that subdomain e.g. site1.localhost.int
, site2.localhost.int
This is pretty much the scheme we have introduced.
因此,如果端点挂在该子域之外,那么*.localhost.int
将为127.0.0.1
网络上的每个人解析为,并且所有开发人员的配置文件设置将“正常工作”,例如site1.localhost.int
,site2.localhost.int
这几乎是我们引入的方案。
dnsmasqalso looks nice, but I have not tried it yet: http://ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/
dnsmasq看起来也不错,但我还没有尝试过:http: //ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/
回答by casivaagustin
I'm using DNSChef to do that.
我正在使用 DNSChef 来做到这一点。
https://thesprawl.org/projects/dnschef/
https://thesprawl.org/projects/dnschef/
You have to download the app, in Linux or Mac you need python to run it. Windows have their own exe.
您必须下载该应用程序,在 Linux 或 Mac 中您需要 python 来运行它。Windows 有自己的 exe。
You must create a ini file with your dns entries, for example
例如,您必须使用 dns 条目创建一个 ini 文件
[A]
*.google.com=192.0.2.1
*.local=127.0.0.1
*.devServer1.com=192.0.2.3
Then you must launch the dns application with admin privileges
然后您必须以管理员权限启动 dns 应用程序
sudo python dnschef.py --file myfile.ini -q
or in windows
或在窗户里
runas dnschef.exe --file myfile.ini -q
Finally you need to setup as your only DNS your local host environment (network, interface, dns or similar or in linux /etc/resolv.conf).
最后,您需要将本地主机环境(网络、接口、dns 或类似或在 linux /etc/resolv.conf 中)设置为您唯一的 DNS。
That's it
就是这样
回答by Stackia
I made this simple tool to take the place of hosts. Regular expressions are supported. https://github.com/stackia/DNSAgent
我制作了这个简单的工具来代替主机。支持正则表达式。 https://github.com/stackia/DNSAgent
A sample configuration:
示例配置:
[
{
"Pattern": "^.*$",
"NameServer": "8.8.8.8"
},
{
"Pattern": "^(.*\.googlevideo\.com)|((.*\.)?(youtube|ytimg)\.com)$",
"Address": "203.66.168.119"
},
{
"Pattern": "^.*\.cn$",
"NameServer": "114.114.114.114"
},
{
"Pattern": "baidu.com$",
"Address": "127.0.0.1"
}
]