我想设置我的本地开发机器,以便任何对*的请求。Local被重定向到localhost。这个想法是,当我开发多个站点时,我只需向Apache添加名为site1的vhosts。地方,site2。让它们都解析到localhost,而Apache则相应地服务于不同的站点。

我用的是Windows XP。

我试着加上

127.0.0.1       *.local

到我的c:\windows\system32\drivers\etc\hosts文件,也试过:

127.0.0.1       .local

这两种方法似乎都不管用。

我知道我可以在不同的端口号上设置它们,但这是一种痛苦,因为很难记住哪个端口是哪个端口。

我不想设置本地DNS服务器,有什么建议吗?


当前回答

下面是那些试图实现目标的人的总配置(开发环境中的通配符,例如XAMPP——这个例子假设所有站点都指向相同的代码库)

Hosts文件(添加一个条目)

文件:% SystemRoot % \ system32 etc, etc drivers \ \ hosts

127.0.0.1   example.local

Httpd.conf配置(启用vhosts)

文件:etc, etc XAMPP \ \ httpd上。

# Virtual hosts
Include etc\extra\httpd-vhosts.conf

httpd-vhosts.conf配置

额外文件:XAMPP \ etc \ \ httpd-vhosts.conf

<VirtualHost *:80>
    ServerAdmin admin@example.local
    DocumentRoot "\path_to_XAMPP\htdocs"
    ServerName example.local
    ServerAlias *.example.local
#    SetEnv APP_ENVIRONMENT development
#    ErrorLog "logs\example.local-error_log"
#    CustomLog "logs\example.local-access_log" common
</VirtualHost>

重新启动apache

创建pac文件:

保存成任何形式。Pac你想在任何地方,然后加载文件在浏览器的网络>代理>auto_configuration设置(重新加载,如果你改变这个)

function FindProxyForURL(url, host) {
  if (shExpMatch(host, "*example.local")) {
    return "PROXY example.local";
  }
  return "DIRECT";
}

其他回答

当您从windows命令提示符运行“ipconfig /flushdns”而不是重新启动计算机时,编辑hosts文件就不那么痛苦了。

@petah和丙烯酸DNS代理是最好的答案,最后他提到了使用Apache做多站点的能力,@jeremyasnyder在下面描述了一点…

... 然而,在我们的例子中,我们正在测试一个多租户托管系统,因此我们想要测试的大多数域都指向同一个虚拟主机,而其他一些域则指向其他地方。

所以在我们的例子中,你只需在ServerAlias指令中使用正则通配符,就像这样…

ServerAlias *.foo.local

我制作了这个简单的工具来代替主机。支持正则表达式。 https://github.com/stackia/DNSAgent

配置示例:

[
    {
        "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"
    }
]

我用Python写了一个简单的dns代理。它将读取/etc/hosts中的通配符条目请看这里:http://code.google.com/p/marlon-tools/source/browse/tools/dnsproxy/dnsproxy.py

我在Linux和Mac OS X上测试过,但还没有在Windows上测试过。

你可以试试AngryHosts,它提供了一种支持通配符和正则表达式的方法。实际上,它是一个主机文件增强和管理软件。 更多功能可以在http://angryhosts.com/features/上看到