【发布时间】:2020-07-16 01:56:17
【问题描述】:
我有一个后端,它在mydomain.com 公开一个普通的 http 服务器。我想要一个安全的端点,所以我决定创建一个 API Gateway 端点,将流量重定向到我的域端点。
这样做时,我会丢失后端公开的所有 CORS 标头。因此,如果直接向后端发送请求,我会得到以下包含 CORS 标头的响应:
HTTP/1.1 200 OK
Access-Control-Allow-Methods: OPTIONS, POST, GET
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 300
Connection: keep-alive
Date: Fri, 03 Apr 2020 20:03:03 GMT
Transfer-Encoding: chunked
foo: bar
Hello World
但是当我向 API Gateway 端点请求时,我得到以下响应:
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 11
Content-Type: text/plain; charset=utf-8
Date: Fri, 03 Apr 2020 20:03:09 GMT
apigw-requestid: KbRzrjEvjoEEMHQ=
foo: bar
我尝试在 API Gateway Cors 面板中设置这些 CORS 标头,但没有成功。
这是我设置 CORS 标头时后端的部分。
app.addListener('request', (req, res) => {
const headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
'Access-Control-Max-Age': 300, // 30 days
'foo': 'bar'
/** add other headers as per requirement */
};
if (req.method === 'OPTIONS') {
res.writeHead(204, headers);
res.end();
return;
}
if (['GET', 'POST'].indexOf(req.method) > -1) {
res.writeHead(200, headers);
res.end('Hello World');
return;
}
res.writeHead(405, headers);
res.end(`${req.method} is not allowed for the request.`);
})
我该如何解决这个问题?
【问题讨论】:
-
您是使用代理还是非代理 HTTP 与 API Gateway 集成?
-
我也有同样的问题。 stackoverflow.com/questions/61215324/… 如果我找到解决方法,我会发表评论。与 AWS 支持联系。我也将这个问题发送给他们。
标签: amazon-web-services ssl cors aws-api-gateway