我想设置我的本地开发机器,以便任何对*的请求。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服务器,有什么建议吗?


当前回答

我使用DNSChef来做这个。

https://thesprawl.org/projects/dnschef/

你必须下载应用程序,在Linux或Mac中,你需要python来运行它。Windows有自己的exe。

例如,您必须创建一个包含dns条目的ini文件

[A]
*.google.com=192.0.2.1
*.local=127.0.0.1
*.devServer1.com=192.0.2.3

然后,您必须以管理员权限启动dns应用程序

sudo python dnschef.py --file myfile.ini -q

或者在Windows中

runas dnschef.exe --file myfile.ini -q

最后,您需要设置为您的本地主机环境(网络,接口,DNS或类似或在linux /etc/resolv.conf)的唯一DNS。

就是这样

其他回答

为了增加这里已经有的伟大建议,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

(对于无法访问主机文件的iOS设备上的测试站点,指定非环回地址的能力非常棒。)

如果您将此配置与其他回答中提到的一些Apache配置相结合,则可以添加无需任何设置的VirtualHosts。

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

如果你阅读手册页,它说:

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文件的代码不支持通配符条目。

解决方法是提前创建所有的条目,可能使用一个脚本来一次性放置几百个条目。

配置nginx配置自动子域与丙烯酸DNS代理

你的nginx sites文件夹的Auto.conf文件

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;
    }
}

添加到Acrylic hosts文件127.0.0.1 example.com *.example.com并重新启动Acrylic服务。 $branch -你的子域名。

设置代替根/var/www/html/$branch/public;您的项目路径

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

但是,如果您只想更改hosts文件以使新站点工作....你可以这样配置你的Apache,你不需要一直编辑它的配置:

http://postpostmodern.com/instructional/a-smarter-mamp/

基本上是基于我的设置的一个快速总结,在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>

这允许我添加这样的条目:

127.0.0.1       test.dev

然后创建目录c:\xampp\sites\dev\test,并将必要的文件放在那里,它就可以工作了。

另一种选择是在apache.conf中使用<Directory>标记,并引用http://localhost/project/中的页面。

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