在 AWS 中连接 HTTPS 和 HTTP

问题描述 投票:0回答:1

我已经将我的前端(React)部署到 Amplify,并且还自己将我的后端(Springboot)部署到 EC2。

我已经测试过,我可以将值发送到我的后端(EC2),它工作正常,如附图所示。 我能够将值从本地主机发送到 EC2 服务器。

问题是 Amplify 是 HTTPS,而我的 EC2 是 HTTP。您能帮我看看如何连接 Amplify 和 EC2 吗?由于我是 AWS 部署新手,我需要您的提示。

非常感谢。 在此输入图片描述

amazon-web-services amazon-ec2 deployment aws-amplify
1个回答
0
投票

我不久前遇到了这个问题。如果您使用 Ampify 或在 S3 存储桶/Cloud Front 中部署 React,则需要确保端点是 HTTPS。您不能使用http。

我最终做的解决方案是创建一个 AWS Lambda 函数,然后通过 API Gateway 连接到 Lambda 函数。 API网关公开的URL是https。

现在我的 React 应用程序可以使用 Fetch API 对 API 网关端点进行成功调用。例如:

const fetchData = async () => {
    try {
      setFetchingData(true);
      const requestOptions = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ status: 'Approved' }), // Corrected status value
      };

      const response = await fetch(
        'https://vrj1xxxxxxxxx.execute-api.us-east-1.amazonaws.com/reditems',
        requestOptions
      );
      
      if (!response.ok) {
        throw new Error('Failed to retrieve item data');
      }

所以它

React 客户端 -> API 网关 -> Lambda 函数 - AWS 服务

© www.soinside.com 2019 - 2024. All rights reserved.