简体   繁体   English

如何在 Laravel 中自动创建子域

[英]How to create a subdomain automatically in Laravel

I am creating a SaaS app using Laravel and I am trying to create subdomains for each user signup.我正在使用 Laravel 创建一个 SaaS 应用程序,并且我正在尝试为每个用户注册创建子域。 For example,例如,

  1. User comes to www.somewebsite.com and signs up用户访问www.somewebsite.com并注册
  2. During the signup, the user provides their business name eg mybiz在注册期间,用户提供他们的公司名称,例如 mybiz
  3. I want my Laravel app to dynamically create a dynamic subdomain mybiz.somewebsite.com我希望我的 Laravel 应用程序动态创建动态子域mybiz.somewebsite.com
  4. Whenever user logs into the website they are automatically redirected to mybiz.somewebsite.com and they stay on this URL during the entire session.每当用户登录网站时,他们都会自动重定向到mybiz.somewebsite.com并且他们在整个 session 期间停留在这个 URL 上。

At no time I as an admin would like to create a subdomain manually.作为管理员,我绝不想手动创建子域。 I am using Apache on Ubuntu for my Laravel install.我在 Ubuntu 上使用 Apache 进行 Laravel 安装。

First create a wildcard subdomain on your server so your laravel app handles all subdomain requests.首先在您的服务器上创建一个通配符子域,以便您的 laravel 应用程序处理所有子域请求。 Then create a global Middleware for your app which extracts the subdomain from the request url and process as appropriate.然后为您的应用程序创建一个全局中间件,该中间件从请求 url 中提取子域并根据需要进行处理。 That way, both https://example.com and https://{username}.example.com is processed by your app and you can do what you wish with the {username} That way, both https://example.com and https://{username}.example.com is processed by your app and you can do what you wish with the {username}

There are many ways to do it, a lot depends on what you are trying to achieve here.有很多方法可以做到这一点,很大程度上取决于您在这里要实现的目标。

I assume you don't want to dynamically create and give away real host names/subdomains, so I would go with creating wildcard DNS record pointing to the machine.我假设您不想动态创建和放弃真实的主机名/子域,所以我会 go 创建指向机器的通配符 DNS 记录。 Single A record pointing to IP address will do the trick, simply add asterisk "*" in front instead of specifying a hostname.指向 IP 地址的单个 A 记录就可以解决问题,只需在前面添加星号"*"而不是指定主机名。

Domain records would then look like this:域记录将如下所示:

┌──────────────────────────────┬─────────────┬──────────────────────────────────┐
│    HOSTNAME                  │    TYPE     │    POINT TO                      │
│──────────────────────────────│─────────────│──────────────────────────────────│
│    some-domain.com           │      A      │    SERVER IP ADDRESS             │
│    *.some-domain.com         │      A      │    SERVER IP ADDRESS             │
│    wwww.some-domain.com      │    CNAME    │    some-domain.com               │

With that you basically redirect all traffic from any variation of the hostname to single address and allow it to manage it.这样,您基本上将所有流量从主机名的任何变体重定向到单个地址并允许它管理它。 Wildcard record like that can be overriden only by another more specific record, so it will identify as whatever.some-domain.com and anything.some-domain.com and allow you to fully manage it virtually without the need to add any more records.像这样的通配符记录只能被另一个更具体的记录覆盖,因此它将标识为whatever.some-domain.comanything.some-domain.com并允许您几乎完全管理它而无需添加任何更多记录.

Now you want to tell your web server to take control over all those possible addresses.现在您想告诉您的 web 服务器控制所有这些可能的地址。 With Apache you can do that by adding new virtual hosts to configuration, as many as you like and need, or you can add just one general and route everything to your Laravel application and let it handle it all on it's own.使用 Apache,您可以通过将新的虚拟主机添加到配置中来做到这一点,只要您喜欢和需要就可以,或者您可以只添加一个通用主机并将所有内容路由到您的 Laravel 应用程序,让它自己处理所有事情。 Something like that would work:像这样的东西会起作用:

 <VirtualHost *:80>
   ServerName your-domain.com
   ServerAlias *.your-domain.com
   DocumentRoot /var/www/laravel/app
 </VirtualHost>

At this point you can do whatever you like - because you handle all the traffic you can route visitors to any subdomain.在这一点上,您可以做任何您喜欢的事情 - 因为您处理所有流量,您可以将访问者路由到任何子域。 You can setup some environmental variables in Apache to know exactly what subdomain was used when someone visit the app, or use PHP built-ins and read the address with globals like $_SERVER['HTTP_HOST'] .您可以在 Apache 中设置一些环境变量,以准确了解当有人访问应用程序时使用了哪个子域,或者使用 PHP 内置插件并使用$_SERVER['HTTP_HOST']等全局变量读取地址。

I am sure Laravel provides more than enough utilities to handle everything out of the box, but there is no need to depend on frameworks, handling the traffic can be done even with pseudo-code like this:我确信 Laravel 提供了足够多的实用程序来处理开箱即用的所有内容,但无需依赖框架,即使使用如下伪代码也可以处理流量:

<?php
if ($_SERVER['HTTP_HOST'] !== CurrentUser::get_personal_subdomain()) {
  http_response_code(301);
  header('Location: '. Router::random_address());
  exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM