如何用同一个域名部署前后端?
How to deploy frontend and backend with the same domain name?
我不是很熟悉部署和网络,因为我主要是一名前端开发人员。我想用 Laravel 和 React(分开的,不与 blade 集成)创建一个项目,并将它们部署到 AWS。我只想将 Laravel 用作 API 服务器,并且我计划将其部署在 EC2 上。如果我在 S3 上托管我的 React 应用程序,我怎么可能与 EC2 实例上的 API 服务器 运行 共享同一个域?
我知道我可以有单独的子域,比如 www.example.com for my React app and api.example.com for my API server. However, if I want to have www.example.com for my React app and www.example.com/api 用于我的 API 服务器,我有什么选择?您可以为我推荐哪些资源以加快了解该主题的速度?谢谢!
如果您想使用 S3 和 EC2,您需要使用一种可以根据条件分发到两个端点的服务。
最好的服务是 CloudFront, which supports distribution to S3 and EC2 (as a custom origin)。
为此,您需要使用 origin for the S3 bucket, and an origin for the API. As your API is hosted on the /api/*
path you would add this as the path pattern when adding the secondary origin via a behaviour.
创建您的发行版
CloudFront 然后会将任何请求路由到 /api/*
路径到您的 EC2 来源。
我找到了一篇名为 How to route to multiple origins with CloudFront 的文章,我希望它能更详细地解释完成此操作的步骤。
我不是很熟悉部署和网络,因为我主要是一名前端开发人员。我想用 Laravel 和 React(分开的,不与 blade 集成)创建一个项目,并将它们部署到 AWS。我只想将 Laravel 用作 API 服务器,并且我计划将其部署在 EC2 上。如果我在 S3 上托管我的 React 应用程序,我怎么可能与 EC2 实例上的 API 服务器 运行 共享同一个域?
我知道我可以有单独的子域,比如 www.example.com for my React app and api.example.com for my API server. However, if I want to have www.example.com for my React app and www.example.com/api 用于我的 API 服务器,我有什么选择?您可以为我推荐哪些资源以加快了解该主题的速度?谢谢!
如果您想使用 S3 和 EC2,您需要使用一种可以根据条件分发到两个端点的服务。
最好的服务是 CloudFront, which supports distribution to S3 and EC2 (as a custom origin)。
为此,您需要使用 origin for the S3 bucket, and an origin for the API. As your API is hosted on the /api/*
path you would add this as the path pattern when adding the secondary origin via a behaviour.
CloudFront 然后会将任何请求路由到 /api/*
路径到您的 EC2 来源。
我找到了一篇名为 How to route to multiple origins with CloudFront 的文章,我希望它能更详细地解释完成此操作的步骤。