同一域上的 CloudFront 静态网站和 EC2 API [英] CloudFront static website and EC2 API on same domain

查看:23
本文介绍了同一域上的 CloudFront 静态网站和 EC2 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的静态网站托管在 CloudFront 上.

I have my static website hosted on CloudFront.

有没有办法让我对 EC2 实例的 API 调用使用相同的域名但使用不同的端口?

Is there a way for me to use the same domain name but different port for API calls to an EC2 instance?

例如.example.com:3000 应该重定向到 EC2 实例,而 example.com(又名 example.com:443example.com:80) 应重定向到 CloudFront.

Eg. example.com:3000 should be redirected to the EC2 instance, while example.com (aka example.com:443 or example.com:80) should be redirected to CloudFront.

推荐答案

无法根据端口号识别目标主机.DNS 名称单独决定了您的浏览器连接到的位置.

It's not possible to discern the target host based on port number. The DNS name alone determines where your browser connects to.

但是,您仍然可以实现您想要的,只是不能通过端口号.

However, you can still achieve what you want, just not by port number.

您可以将 CloudFront 分配配置为根据 URL 中的路径将请求转发到您的静态网站或 API.

You can configure your CloudFront distribution to forward requests to your static website or API depending on the path in the URL.

为此,您需要在 CloudFront 分配中配置多个源和多个行为:

To accomplish this, you would configure multiple origins and multiple behaviours in your CloudFront distribution:

  • /api/* -> EC2 来源
  • 其他一切 -> 静态网站来源

在您的浏览器中,它显示为一个网站.当您请求 https://www.example.com/page1.html 时,CloudFront 会将请求转发到静态网站.当您请求 https://www.example.com/api/MyRestMethod 时,CloudFront 会将请求转发到您的 EC2.

To your browser, it appears as a single website. When you request https://www.example.com/page1.html, CloudFront will forward the request to the static webiste. When you request https://www.example.com/api/MyRestMethod, CloudFront will forward the request to your EC2.

这还允许您在 API 和静态网站之间为 HTTPS 使用相同的 ACM 证书.

This also allows you to use the same ACM certificate for HTTPS between your API and your static website.

这篇关于同一域上的 CloudFront 静态网站和 EC2 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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