Cloudfront + ELB 可以更改我请求的查询字符串吗?

Can Cloudfront + ELB change the query string of my request?

我们是 运行 AWS 上的 React Web 应用程序。 Web 应用程序由 S3 存储桶托管,API 位于弹性 beantalk 上。我们使用 Cloudfront 将其整合到一个域中。

到目前为止一切顺利。站点加载,它可以与 API.

对话

问题是,到达我们的弹性 beantalk 实例的所有 API 请求在查询字符串中仍然有 /api。这对我们的 API 很好,因为我们可以控制它,但我们正在部署 tileserver-gl 的实例,它不允许我们配置根 url 来提供服务。

我似乎无法配置 Cloudfront 来修改查询字符串以截断第一部分。例如。这样 mysite.com/api/v1/users 就会映射到 fj935hf02.elasticbeanstalk.com/v1/users.

其他人是如何规避这个问题的?

是的,CloudFront 可以通过 Lamdba@edge 进行类似的修改。具体来说,您可以查看 Origin request 函数,它可以修改传递给原点的内容。

A​​WS 还提供了 examples of such functions. One 示例,展示了如何使用查询字符串。

'use strict';

const querystring = require('querystring');
exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    
    /* When a request contains a query string key-value pair but the origin server
     * expects the value in a header, you can use this Lambda function to
     * convert the key-value pair to a header. Here's what the function does:
     * 1. Parses the query string and gets the key-value pair.
     * 2. Adds a header to the request using the key-value pair that the function got in step 1.
     */

    /* Parse request querystring to get javascript object */
    const params = querystring.parse(request.querystring);

    /* Move auth param from querystring to headers */
    const headerName = 'Auth-Header';
    request.headers[headerName.toLowerCase()] = [{ key: headerName, value: params.auth }];
    delete params.auth;

    /* Update request querystring */
    request.querystring = querystring.stringify(params);

    callback(null, request);
};