【问题标题】:Automatically create a sub-domain for laravel application using wild card dns使用通配符 dns 为 laravel 应用程序自动创建子域
【发布时间】:2017-11-12 06:34:46
【问题描述】:

我正在使用 laravel5.5,我正在实现通配符 dns,为每个用户提供自己的子域。

在我的实现中,我正在使用装有 Windows 10 和 laragon 服务器的笔记本电脑。

在 laragon 我这样设置

<VirtualHost *:80>
    DocumentRoot "C:/laragon/www/tindahan/public/"
    ServerName tindahan.local
    ServerAlias *.tindahan.local
    <Directory "C:/laragon/www/tindahan/public/">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

在主机文件上

127.0.0.1      tindahan.local       
127.0.0.1      fil.tindahan.local   
127.0.0.1      liz.tindahan.local     

在路上

Route::get('/', function () {
  $url       = parse_url(url()->current());
  $domain    = explode('.', $url['host']);
  $subdomain = $domain[0];

  dd($subdomain);
});

所以当访问 fil.tindahan.local 或 liz.tindahan.local 时,我得到了结果

filLiz

但是如果我访问 joseph.tindahan.local 我得到了这个错误

无法访问此网站

我需要在主机上创建另一个子域才能使其正常工作。

问题是,怎么能自动做到呢?

当我输入任何名称作为子域时,它应该会自动创建,这样我就永远不会手动创建?

【问题讨论】:

    标签: linux host wildcard-subdomain dnsmasq


    【解决方案1】:

    据我所知,错误将在网络层解决,而不是从应用层解决(在本例中为 laravel)。

    我认为您需要自己的本地 DNS 服务器。

    我在 Linux (ubuntu) 中使用过dnsmasq,它运行良好。

    步骤:

    1. apt install dnsmasq
    2. 用编辑器打开/etc/dnsmasq.conf
    3. 添加address=/your_domain.loc/127.0.0.1(这只是一个示例,因此您可能需要更改domainip)。
    4. 运行sudo systemctl restart dnsmasq重启服务。
    5. 好吧,您可以在浏览器中测试来自 your_domain.loc 的任何 url,您不会收到该错误 ;)

    祝你好运

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 2018-04-11
      • 2016-09-14
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2013-01-21
      • 2012-11-14
      • 2016-03-14
      相关资源
      最近更新 更多