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

查看:55
本文介绍了同一域上的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:443 example.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天全站免登陆