Windows主机文件中的通配符

Windows主机文件中的通配符,windows,apache,dns,hosts,virtual-hosts,Windows,Apache,Dns,Hosts,Virtual Hosts,我想设置本地开发计算机,以便对*.local的任何请求都被重定向到localhost。我的想法是,当我开发多个站点时,我只需向Apache添加名为site1.local、site2.local等的vhost,并将它们全部解析为localhost,而Apache则相应地为不同的站点提供服务 我使用的是Windows XP 我试着加上 127.0.0.1 *.local 到我的c:\windows\system32\drivers\etc\hosts文件,也尝试了: 127.0.0.1

我想设置本地开发计算机,以便对
*.local
的任何请求都被重定向到
localhost
。我的想法是,当我开发多个站点时,我只需向Apache添加名为
site1.local
site2.local
等的vhost,并将它们全部解析为
localhost
,而Apache则相应地为不同的站点提供服务

我使用的是Windows XP

我试着加上

127.0.0.1       *.local
到我的
c:\windows\system32\drivers\etc\hosts
文件,也尝试了:

127.0.0.1       .local
两者似乎都不起作用

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


我不想设置本地DNS服务器或任何困难的事情,有什么建议吗?

我认为这是不可能的

无论如何,每次添加新站点和位置时都必须修改apache virtualroot条目,因此将新名称同步到Windows vhost文件并不是一项大的工作


更新:请检查下一个答案以及对此答案的评论。这个答案已经6年了,现在已经不正确了。

你可以说服你的网络管理员为你设置一个域(比如说'evilpuppetmaster.hell'),并在那里设置通配符,这样所有(*.evilpuppetmaster.hell')都可以解析为你的IP。

我发现了一个帖子,上面也写着“不允许使用通配符”


在过去,我只是在hosts文件中添加了额外的条目,因为(如前所述),当您已经在编辑apache配置文件时,不会有太多额外的工作。

要回答您的问题,您不能在Windows下的hosts文件中使用通配符

但是,如果您只想更改主机文件以使新站点正常工作。。。。您可以这样配置Apache,而不必一直编辑其配置:

基本上是一个基于我的设置的快速摘要,将以下内容添加到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>
然后创建目录c:\xampp\sites\dev\test并将必要的文件放在其中,它就可以正常工作了


另一个选项是在apache.conf中使用
标记并引用中的页面。

我找不到书面禁止,但按照惯例,Windows主机文件紧跟UNIX主机文件,您不能将通配符主机名引用放入该文件中

如果您阅读手册页,它会说:

DESCRIPTION
     The hosts file contains information regarding the known hosts on the net-
     work.  For each host a single line should be present with the following
     information:

           Internet address
           Official host name
           Aliases
虽然它确实说

     Host names may contain any printable character other than a field delim-
     iter, newline, or comment character.
从实际层面来看,这是不正确的

基本上,查看/etc/hosts文件的代码不支持通配符条目


解决方法是提前创建所有条目,可以使用脚本一次放入几百个条目。

您可以使用动态DNS客户端,例如。然后,使用外部DNS服务器CNAME*.mydomain.com到mydomain.no ip.com。

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

我用Python编写了一个简单的DNS代理。它将读取/etc/hosts中的通配符条目。请看这里:

我已经在Linux和Mac OS X中进行了测试,但还没有在Windows中进行测试。

您可以尝试,它提供了一种支持通配符和正则表达式的方法。实际上,它是一个主机文件增强和管理软件。
可以看到更多功能@

丙烯酸DNS代理(免费、开源)完成了这项工作。它使用自己的主机文件创建代理DNS服务器(在您自己的计算机上)。主机文件接受通配符

从官方网站下载

配置DNS代理 要配置DNS代理,请从上面的链接安装,然后转到:

127.0.0.1   *.localhost
127.0.0.1   *.local
127.0.0.1   *.lc
Preferred DNS Server: 127.0.0.1
  • 开始
  • 节目
  • 丙烯酸DNS代理
  • 配置
  • 编辑自定义主机文件(AcrylicHosts.txt)
  • 在文件末尾添加以下行:

    127.0.0.1   *.localhost
    127.0.0.1   *.local
    127.0.0.1   *.lc
    
    Preferred DNS Server: 127.0.0.1
    
    重新启动DNS代理服务:

    127.0.0.1   *.localhost
    127.0.0.1   *.local
    127.0.0.1   *.lc
    
    Preferred DNS Server: 127.0.0.1
    
  • 开始
  • 节目
  • 主动DNS代理
  • 配置
  • 重新启动服务
  • 您还需要在网络接口设置中调整DNS设置:

    127.0.0.1   *.localhost
    127.0.0.1   *.local
    127.0.0.1   *.lc
    
    Preferred DNS Server: 127.0.0.1
    
  • 开始
  • 控制面板
  • 网络与因特网
  • 网络连接
  • 局部区域连接属性
  • TCP/IPv4
  • 设置“使用以下DNS服务器地址”:

    127.0.0.1   *.localhost
    127.0.0.1   *.local
    127.0.0.1   *.lc
    
    Preferred DNS Server: 127.0.0.1
    

    如果你将这个答案与jeremyasnyder的答案结合起来(使用
    VirtualDocumentRoot
    ),你就可以通过创建一个目录自动设置域/虚拟主机。

    虽然你不能添加这样的通配符,但你可以添加你需要的完整站点列表,至少用于测试,这对我来说已经足够好了,在主机文件中,您只需添加:

    127.0.0.1站点1.本地
    127.0.0.1站点2.本地
    127.0.0.1站点3.本地

    我们在本地DNS服务器中使用通配符DNS进行此工作:添加一个
    A
    记录,如
    *.local->127.0.0.1

    我认为您的网络设置需要在网络上机器的域后缀搜索列表中包含所选的域后缀,因此您可能希望用公司的内部域(例如
    .int
    )替换
    .local
    ),然后添加一个子域,如
    .localhost.int
    ,以明确其用途

    因此,
    *.localhost.int
    将为网络上的每个人解析为
    127.0.0.1
    ,如果端点挂起该子域,所有开发人员的配置文件设置将“正常工作”,例如
    site1.localhost.int
    site2.localhost.int
    ,这就是我们引入的方案

    dnsmasq看起来也不错,但我还没有试过:

    @petah和丙烯酸DNS代理是最好的答案,最后他提到了使用Apache进行多站点操作的能力,这是@jeremyasnyder描述的
    function FindProxyForURL(url, host) {
      if (shExpMatch(host, "*example.local")) {
        return "PROXY example.local";
      }
      return "DIRECT";
    }
    
    [
        {
            "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"
        }
    ]
    
          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
    
    server {
        listen 80;
        server_name ~^(?<branch>.*)\.example\.com;
        root /var/www/html/$branch/public;  
    
        index index.html index.htm index.php;
    
        charset utf-8;
    
        location / {
            try_files $uri $uri/ /index.php$is_args$args;
        }
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
    
        error_log  /var/log/nginx/$branch.error.log error;
    
        sendfile off;
    
        client_max_body_size 100m;
    
        location ~ \.php$ {
           try_files $uri /index.php =404;
           fastcgi_pass php-fpm:9000;
           fastcgi_index index.php;
           fastcgi_buffers 16 16k;
           fastcgi_buffer_size 32k;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    
    nano /etc/hosts
    pihole restartdns
    
    127.0.1.1       raspberrypi
    192.168.1.1     w1.dev.net
    192.168.1.2     w2.dev.net
    192.168.1.3     w3.dev.net