【发布时间】:2020-10-13 09:51:35
【问题描述】:
我正在使用 nginx 对 AWS API Gateway 阶段进行反向代理。这很简单:
location /api {
proxy_pass https://xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com:443/production;
proxy_ssl_server_name on;
}
但是,当 xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com 的 DNS 条目发生变化时,这种方法会使 nginx 在上游服务过时,因为它会在启动时解析该条目。
在这篇文章之后:https://www.nginx.com/blog/dns-service-discovery-nginx-plus/ 我现在正在尝试在这样的变量中定义我的代理目标:
location /api {
set $apigateway xxxxxxxxxx.execute-api.eu-central-1.amazonaws.com/production;
proxy_pass https://$apigateway:443/;
proxy_ssl_server_name on;
}
这将使 API Gateway 以 ForbiddenException: Forbidden 响应将通过先前设置而不使用变量的请求。阅读本文档:https://aws.amazon.com/de/premiumsupport/knowledge-center/api-gateway-troubleshoot-403-forbidden/ 它告诉我这可能是 WAF 过滤我的请求(当该 API 未启用 WAF 时),也可能是私有 API 缺少主机标头(当 API 是公共时)。
我想我可能做错了一件事情:
- 用于设置变量的语法错误
- 使用该变量会使 nginx 向 API Gateway 发送不同的标头,我需要手动干预。我确实尝试过设置
Host标头,但没有任何区别。
使用的nginx版本是1.17.3
【问题讨论】:
-
我认为该问题与this nginx ticket 和this commit 有关。本质上,如果您使用带有 proxy_pass 的变量,那么它将忽略尾部反斜杠的行为。解决它的唯一方法似乎是使用像接受的答案一样的 URL 重写。
标签: nginx aws-api-gateway