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


当前回答

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

其他回答

配置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;您的项目路径

我们在本地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看起来也不错,但我还没有尝试过: http://ihaveabackup.net/2012/06/28/using-wildcards-in-the-hosts-file/

我发现一个关于使用Windows主机文件的帖子也说“不允许通配符”。

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

我制作了这个简单的工具来代替主机。支持正则表达式。 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"
    }
]

我认为这是不可能的。

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

更新:请查看下一个答案和对这个答案的评论。这个答案是6年前的了,不再正确了。