【问题标题】:How to put wildcard entry into /etc/hosts?如何将通配符条目放入 /etc/hosts?
【发布时间】:2013-12-07 21:32:20
【问题描述】:

我最近想将所有子域指向一个测试域,比如说 example.com 到 localhost。有没有办法将 *.example.com 上的所有请求都指向 127.0.0.1

【问题讨论】:

标签: hosts


【解决方案1】:

/etc/hosts 文件恰好不支持通配符条目。

您必须使用其他服务,例如 dnsmasq。要在 dnsmasq 中启用它,只需编辑 dnsmasq.conf 并添加以下行:

address=/example.com/127.0.0.1

【讨论】:

  • 用于通配符条目 - address=/.example.com/127.0.0.1
  • @Vivek 不需要
  • 对于 ubuntu dnsmasq 设置参见 this one
  • 对于 macosx 设置 passingcuriosity.com/2013/dnsmasq-dev-osx 非常有帮助。
  • rahilwazir,实际上,如果您不想要所有子域。 # 也可用作通配符。我个人使用 `address=/dev#.example.com/127.0.0.1 使 dev123.example.com 等工作。
【解决方案2】:

这里是那些试图实现原始目标的配置(通配符都指向同一个代码库——什么都不安装,开发环境,即 XAMPP)

hosts 文件(添加条目)

文件:/etc/hosts(非 Windows)

127.0.0.1   example.local

httpd.conf 配置(启用虚拟主机)

文件:/XAMPP/etc/httpd.conf

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

httpd-vhosts.conf 配置

文件:XAMPP/etc/extra/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 文件:

将文件保存为whatever.pac,然后在浏览器的网络中加载该文件>代理>自动配置设置(如果您更改此设置,请重新加载)

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

【讨论】:

  • 不适用于 Windows 上的 hosts 文件。它只会接受 www.example.com
  • 这确实适用于在 Firefox 中使用 pac 文件的 Windows。我已经在多台 Windows 机器上对此进行了测试。 hosts 文件只需要一个条目,pac 文件处理子域。
  • 据我了解,最初的问题根本与 apache 无关,而是在网络级别,所以我认为您的回答没有抓住重点:/
  • 在 Internet Explorer 中使用 pac 文件见:campus.barracuda.com/product/websecurityservice/article/WSS/…
  • 请注意,当使用 pac 文件将请求代理到主机(例如 localhost)时,像 Apache 这样的 Web 服务器将使用 REQUEST_URI 变量中的完整 URL(包括协议和主机名),而不仅仅是路径组件。
【解决方案3】:

使用 dnsmasq

假设您使用的是基于 Debian 的 dist(ubuntu, mint..),请检查它是否安装了

(sudo) systemctl status dnsmasq

如果它刚刚被禁用,请使用

(sudo) systemctl start dnsmasq

如果一定要安装,写

(sudo) apt-get install dnsmasq

定义域以解析编辑/etc/dnsmasq.conf 像这样。

address=/example.com/127.0.0.1

解决 *.example.com

!您必须重新加载 dnsmasq 才能使更改生效!

systemctl reload dnsmasq

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2014-10-20
    相关资源
    最近更新 更多