使用http2时不会出现此问题,似乎是http1.1特有的。
给定一个带有以下代码的 aws nodejs 18 lambda
export const healthHandler = async (event) => {
return {
body: `ok! origin: '${event.headers['origin']}'`
}
}
使用无服务器部署到 aws api 网关中
service: gateway-cors
provider:
name: aws
stage: test
runtime: nodejs18.x
functions:
health:
handler: index.healthHandler
events:
- http:
method: GET
path: health/{any+}
cors: true
如果通过 http2 发出网络请求,我得到
curl https://<id>.execute-api.eu-west-1.amazonaws.com/test/health/test -H "Origin: https://test.com" --http2
ok! origin: 'https://test.com'
但是如果我通过 http1.1 成功,我就会得到
curl https://<id>.execute-api.eu-west-1.amazonaws.com/test/health/test -H "Origin: https://test.com" --http1.1
ok! origin: 'undefined'
具有完整重现的回购可在此处获取
有谁知道为什么 aws api gateway 不通过 http1.1 传递原始标头,有没有办法让它传递原始标头?
事实证明,这是因为当您使用 http2 时,网关会重写标头以遵循 http2 标头规范。对于 http1.1,仍然提供标头,但它使用其原始名称“Origin”,大写 O。
将返回更改为
'${event.headers['origin'] ?? event.headers['Origin']}'
可以澄清情况。