【问题标题】:laravel 5.2, wildcard subdomainslaravel 5.2,通配符子域
【发布时间】:2016-07-08 02:36:51
【问题描述】:

我想要这样的路线:

john-doe.mysite.local
john-another-doe.mysite.local

然后将子域名传递给某个 controller@method 并呈现用户配置文件。

我正在使用Laravel Homestead

这是我本地机器上的/etc/hosts 文件:

....
192.168.10.10 mysite.local
192.168.10.10 *.mysite.local
....

在我的虚拟机上/etc/nginx/sites-available/mysite.local 文件

server {
    listen 80;
    server_name mysite.local *.mysite.local;
    root "/home/vagrant/laravel-projects/mysite.com/public";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    ....

如果您需要其他任何东西,请询问。 主域mysite.local正常工作。

我尝试关注documentation 并将其添加到我的routes.php 文件中:

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('/', function ($element) {
        dd($element);
    });

});

但是如果我去任何子域somedomain.mysite.local 我找不到服务器。

在我看来,问题在于我什至没有通过子域访问 Laravel 应用程序。

感谢任何帮助。 我找到了this question,并尝试在我的/etc/hostsmysite.local 文件中将.local 更改为.dev,但没有成功。我也尝试将mysite.local 更改为mysite.dev,但也没有用。

我不知道我在这里错过了什么。

更新:

如果我将test.mysite.local 添加到我的/etc/hosts 文件中,我会在浏览器中得到与浏览mysite.local 相同的输出。

....
192.168.10.10 mysite.local
192.168.10.10 test.mysite.local
192.168.10.10 *.mysite.local
....

更新虚拟机上的mysite.local

...
server_name mysite.local test.mysite.local *.mysite.local;
...

我尝试添加另一条路由到routes.php

Route::group(['domain' => 'test.mysite.local'], function() {

    Route::get('/', function () {
        dd('you are at test subdomain');
    });

});

要查看它是否有效但无效,我仍然会得到与导航到 mysite.local 相同的输出

另一个更新:

/etc/hosts/ 不支持通配符,所以目前我使用的是静态子域名,稍后会弄清楚如何使用动态子域名。

也放

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('/', function ($element) {
        dd($element);
    });

});

置顶或您的routes.php 文件。

目前我正在尝试将静态子域重定向到特定的控制器@方法。会随着我的进展更新。

更新 - 静态域现在工作:

新路线:

Route::group(['domain' => '{element}.mysite.local'], function() {

    Route::get('{path}', 'ElementController@single')->where('path', '.*');

});

ElementController.php

public function single($element)
{
    //return view with information about my element
    if ( $element = Element::where('slug', $element)->first() )
    {
        return view('elements.single', compact('element'));
    }

    //redirect to mysite.local if slug doesn't exist
    return redirect(Config::get('app.url')); // points to config/app.php and find `url` parameter which should be defined in your `.env` file

}

待办事项:

  • slug.mysite.local/sadsadas 从上面工作并运行 single 方法,我 如果它会重定向到slug.mysite.local,我会更喜欢
  • 通配符子域仍然无法正常工作

【问题讨论】:

    标签: nginx laravel-5.2 wildcard-subdomain


    【解决方案1】:

    也许是这个? 168.168.10.10 在您的主机中......它似乎必须是 192.168.10.10

    【讨论】:

    • 没什么,但现在我需要再次尝试将.local 更改为.dev,因为我不确定这个错字是什么时候出现的。谢谢!
    • 我尝试将hostsmysite.local 文件中的.local 更改为.dev。我也将mysite.local 复制到mysite.dev,但它没有用。但是,这带来了另一个问题,那就是当我在浏览器中打开 mysite.dev 时,我看到了另一个我在本地拥有的站点,该站点也在我的 hosts 文件中定义。即使我在hosts 文件中对其进行了评论,它也会重定向到它。看来我在这里遗漏了一些重要的东西。
    • 你能确认 nginx 配置已经运行良好了吗?我的意思是先尝试使用没有子域的简单域,然后在 laravel 中使用简单的路由.. 看看页面是否打开
    • 是的,TLD 运行良好。这只是我无法访问的子域。
    • 我用192.168.10.10 test.mysite.local 更新了/etc/hosts,当我用浏览器指向它时,我得到的响应与我要去mysite.local 一样。
    猜你喜欢
    • 2021-05-12
    • 2017-06-22
    • 2017-12-14
    • 1970-01-01
    • 2020-08-23
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    相关资源
    最近更新 更多