简体   繁体   English

在 lambda@edge 重写请求云不满足

[英]The request cloud not be satisfied in rewrite at lambda@edge

I want to do rewrite in lambda@egde.我想在 lambda@egde 中重写。

I got an error when I try to browse: https://example.com/foo尝试浏览时出现错误: https://example.com/foo

在此处输入图像描述

This is my function:这是我的 function:

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  
  if (/^\/foo($|\/)/.test(request.uri)) {
    request.uri = "https://api.example.com/bar";
  }
  
  callback(null, request);
};

I want to do rewrite (not redirect).我想重写(而不是重定向)。 What is wrong with this code?这段代码有什么问题?

You're changing the URI to be an absolute path of a users request, it should just be the path part of the request.您正在将 URI 更改为用户请求的绝对路径,它应该只是请求的路径部分。

So the below is what this should become.所以下面是这应该变成的样子。

exports.handler = (event, context, callback) => {
  const request = event.Records[0].cf.request;
  
  if (/^\/foo($|\/)/.test(request.uri)) {
    request.uri = "/bar";
    request.headers['host'] = [{ key: 'host', value: "api.example.com"}];
  }
  
  callback(null, request);
};

If you wanted to change the domain you would need to modify the host header.如果您想更改域,则需要修改主机 header。

Bare in mind with this example it will only rewrite the URI before it goes to the origin.请记住这个例子,它只会在到达源头之前重写 URI。 If you want the users request to change you would need to perform a rewrite.如果您希望用户请求更改,则需要执行重写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM