带有子域的 Laravel 应用程序可以通过 CNAME 链接到其他域 [英] Laravel application with subdomains that can be linked to other domains via CNAME

查看:37
本文介绍了带有子域的 Laravel 应用程序可以通过 CNAME 链接到其他域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似于 Shopify 的应用程序架构.

I'm trying to create an application architecture similar to that of Shopify's.

我从 Forge 运行这个应用程序,并且我已经设置了通配符 DNS 工作所需的配置.

I am running this application from Forge and I've set up the necessary configuration for wildcard DNS to work.

目前人们可以注册我的 Laravel Spark 应用程序,然后会在以下位置为他们动态创建一个子域:

At the moment people can sign up to my Laravel Spark app and then a subdomain will be created dynamically for them at:

Route::group(['domain' => '{company}.app.com'], function () {
    Route::get('/', 'SubdomainController@index');
});

因此,如果他们注册一个帐户并将公司名称设置为 Company,那么他们将在 company.app.com 上创建一个子域.此子域返回包含与其帐户相关的数据的视图(它几乎为我的用户返回自定义网站).

So if they register an account and set the company name as Company then they will have a subdomain created at company.app.com. This subdomain returns a view with data related to their account (It pretty much returns a custom website for my user).

我想让用户能够将他们位于 company.app.com 的网站镜像到他们自己的域地址.我曾尝试在 GoDaddy 中的备用域上设置掩码,但我必须对此进行测试,但它似乎不起作用:( 我在控制台中收到此错误:

I'd like to give users the ability to mirror their site at company.app.com to their own domain address. I have tried setting up a mask within GoDaddy on a spare domain I have to test this however it doesn't seem to work :( I get this error in the console:

Refused to display 'http://company.app.com/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

所以我猜这绝对不是正确的做法.

So I'm guessing this is definitely not the right way to do it.

这件事发生后,我查看了其他公司是如何管理的,看起来他们创建了一个 A 记录和 CNAME,指向他们应用程序的根.Shopify 的指南 解释说,他们将自己的 IP 添加为 A 记录,然后将 CNAME 设置为 shop.myshopify.com 域.

After this happened I looked at how other companies managed this and it looks like they create an A record and CNAME that points to the root of their app. Shopify's guide explains that they add their IP as an A record and then the CNAME is set as the shops.myshopify.com domain.

所以现在我想知道当请求登陆我的应用程序时如何将请求指向正确的方向.例如,如果流量到达 random.com 并且它有一个 A record 等于我的服务器 IP,加上 CNAME>app.com,那么我如何处理请求并将其重定向到正确的子域,同时将用户保留在他们的自定义域中?

So now I'm left wondering how I can point requests in the right direction when they land on my app. So for example if traffic hits random.com and it has an A record that is equal to my servers IP, plus a CNAME of app.com, then how do I handle the request and redirect it to the correct subdomain whilst keeping the user on their custom domain?

谢谢尼克

推荐答案

这要容易得多.您的服务器正在将 X-Frame-Options 标头设置为 SAMEORIGIN,这意味着您网站的内容只能在框架内呈现,前提是该框架在您的域中.

It's much easier than that. Your server is setting the X-Frame-Options header to SAMEORIGIN which means content from your site can only be rendered within a frame if that frame is on your domain.

你要么完全摆脱它,这样任何人都可以在任何网站上构建你的内容,或者你保存你的客户域并动态进行某种数据库查找,或者配置某种特定的 url 供他们使用将哪些代理链接到您服务器上的目录的内容,并具有一部分 url,您可以将其捕获为变量以在服务器配置中使用.然后您将标头设置为仅允许来自他们的域.

You either get rid of that totally, so anyone can frame your content on any site, or you save your customers domains and do some sort of database lookup on the fly, or configure some sort of specific url for them to use for content linking which proxies to their directory on your server and has a part of the url which you can capture as a variable to use in your server config. Then you set your header to allow from their domain only.

因此 embed.myapp.com/example.com 可以提供来自 example.myapp.com 的内容并添加标题

So embed.myapp.com/example.com could serve the content from example.myapp.com and add the header

X-Frame-Options: ALLOW-FROM https://example.com/

这篇关于带有子域的 Laravel 应用程序可以通过 CNAME 链接到其他域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆