AWS API 网关:状态代码 413,请求实体太大

AWS API Gateway: Status Code 413, Request Entity too large

我少了一台服务器 API,带有 AWS API 网关和 Lambda 功能。我正在使用自定义授权功能进行授权。 header 太大,因此出现此错误。通常,对于 nginx 服务器,我会更改 nging 配置,这将得到修复。我不知道如何在 AWS API 网关中处理这个问题。

目前无法更改负载大小的限制。来自 https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html:

10MB payload size, which cannot be changed currently.

如果您想要更多自定义,您应该 运行 您的 API 在实际服务器上(例如使用 Amazon EC2)。

10MB 的负载限制适用于消息 body。如果您 运行 受到 header 大小的限制,很遗憾,这些无法配置。它们在 CloudFront 页面上有说明:http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-limits.html

特别是:

Custom headers: maximum length of a header name 256 characters

Custom headers: maximum length of a header value 2,048 characters

Custom headers: maximum length of all header values and names combined 10,240 characters

我发现压缩内容可以解决这个问题(代码灵感来自此博客 post https://techblog.commercetools.com/gzip-on-aws-lambda-and-api-gateway-5170bb02b543

使用 Typescript 查看此示例:

export async function getResponse(savedItems) {
  return new Promise<APIGatewayProxyResult>((resolve, reject) => zlib.gzip(JSON.stringify(savedItems), (error, gzippedResponse) => {
    if (error) {
      reject(error);
      console.error(error);
    } else {
      const response: APIGatewayProxyResult = {
        statusCode: 200,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
          'Content-Encoding': 'gzip',
        },
        isBase64Encoded: true,
        body: gzippedResponse.toString('base64'),
      };
      console.info(`response statusCode: ${response.statusCode} body length: ${response.body.length}`);
      resolve(response);
    }
  }));
}