Ant*_*ton 1 amazon-web-services aws-api-gateway aws-elb
我正在尝试为以前由 nginx 反向代理处理的内容设置 AWS API 网关。我的端点是 VPC 内的 EC2 实例。我已经进行了设置,以便网关可以访问这些实例。
之前的 nginx 设置如下所示:
http {
server {
listen 80;
location /host1/ {
proxy_pass http://host1:8000/;
}
location /host2/ {
proxy_pass http://host2:8070/;
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试重写请求路径时出现问题。我在 Gateway: 中设置了一条测试路由ANY /test/{proxy+}
,并将其传递给相应的 EC2 实例。我已经验证,请求已通过,但它们包含请求的完整路径:
# machine 1:
curl -v 'https://<endpoint>.amazonaws.com/test/hello_world/test/a'
< HTTP/2 404
< date: Sat, 18 Dec 2021 09:21:42 GMT
< content-type: text/html;charset=utf-8
< content-length: 469
< server: SimpleHTTP/0.6 Python/3.7.10
< apigw-requestid: Kic2FiLIFiAEN_g=
<
--- response ---
# server:
192.168.9.6 - - [18/Dec/2021 09:15:05] "GET /test/hello_world/test/a HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)
(404是预期的,重要的部分是请求到达服务器)
然后,我尝试重写请求路径以/test
使用参数映射删除前导:我指定了“所有传入请求”,要修改的参数:path
,修改类型:overwrite
,值:($request.path.proxy
路由中定义的全部字段)。
现在我收到 400 错误,并且请求不再到达我的服务器:
# machine 1:
curl -v 'https://<endpoint>.amazonaws.com/test/hello_world/test/a'
< HTTP/2 400
< date: Sat, 18 Dec 2021 09:19:53 GMT
< content-type: text/html
< content-length: 122
< server: awselb/2.0
< apigw-requestid: KiclDhxXFiAEMhg=
<
<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
</body>
</html>
# server:
-nothing-
Run Code Online (Sandbox Code Playgroud)
当我将 映射$request.path.proxy
到时querystring.path
,请求而不是path
到达服务器:
# machine 1:
curl -v 'https://<endpoint>.amazonaws.com/test/hello_world/test/a'
< HTTP/2 404
< date: Sat, 18 Dec 2021 09:21:42 GMT
< content-type: text/html;charset=utf-8
< content-length: 469
< server: SimpleHTTP/0.6 Python/3.7.10
< apigw-requestid: Kic2FiLIFiAEN_g=
<
--- response ---
# server:
192.168.9.6 - - [18/Dec/2021 09:21:42] "GET /test/hello_world/test/a?path=hello_world%2Ftest%2Fa HTTP/1.1" 404 -
Run Code Online (Sandbox Code Playgroud)
请注意,查询参数的值path
正是我想要替换原始请求路径的正确值。
这是 AWS 的错误,还是我只是缺少一些文档,表明您无法以这种方式重写路径?值得注意的是,当{proxy+}
路径参数为空时,请求会正确路由......
归档时间: |
|
查看次数: |
1143 次 |
最近记录: |