【发布时间】:2019-04-24 12:07:02
【问题描述】:
我有一个带有目标组的 ALB 和运行 PHP API 的 ECS 集群。
我正在尝试查询 API 以获取 CSV 响应,但如果请求来自 ALB,我会得到截断的结果。
当我通过 SSH 连接到运行集群的 EC2 实例并尝试手动运行 curl(通过负载均衡器)时,响应会被截断:
curl -sSL -D - 'https://my.domain.com/api/export?token=foobar&start_date=01-01-2015&end_date=01-01-2019' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' -o /dev/null
我得到了这些标题:
HTTP/2 200
date: Wed, 21 Nov 2018 20:25:27 GMT
content-type: text/csv; charset=utf-8
content-length: 173019
server: nginx
content-transfer-encoding: binary
content-description: File Transfer
content-disposition: attachment;filename=export.csv
cache-control: private, must-revalidate
etag: "b90d0da7b482da96e1a478d59eedd0d16552fbfd"
strict-transport-security: max-age=2592000; includeSubDomains; preload
content-security-policy-report-only: default-src 'self';
x-frame-options: DENY
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
referrer-policy: origin
curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)
如果我尝试对容器运行相同的 curl(在本地运行 - 不是通过 ALB)
curl -sSL -D - 'http://localhost:32776/api/export?token=foobar&start_date=01-01-2015&end_date=01-01-2019' \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' -o /dev/null
回复:
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/csv; charset=utf-8
Content-Length: 173019
Connection: keep-alive
Content-Transfer-Encoding: binary
Content-Description: File Transfer
content-disposition: attachment;filename=export.csv
Cache-Control: private, must-revalidate
Date: Wed, 21 Nov 2018 20:36:55 GMT
ETag: "b90d0da7b482da96e1a478d59eedd0d16552fbfd"
Strict-Transport-Security: max-age=2592000; includeSubDomains; preload
Content-Security-Policy-Report-Only: default-src 'self;
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Referrer-Policy: origin
当我比较它们时,HTTP 版本有所不同。我尝试在 ALB 中切换到 HTTP1,但仍然遇到相同(或类似)的问题:curl: (18) transfer closed with 130451 bytes remaining to read。
另一个区别是Keep-Alive 选项。我不确定这是否是我可以在 ALB 上启用的属性。
当我尝试返回不同的响应(复杂的网页/非常长)时,响应通过 ALB 没有问题(未截断)。根据 ALB 启用 HTTP/1.1 时的错误消息,每次 42568 字节后都会截断响应。
有什么想法吗?
更新
如果我在响应中省略了 Content-Type 标头,它不会被截断。
return new Response($content, Response::HTTP_OK, [
# Works without this:
# 'Content-Type' => 'text/csv; charset=utf-8',
'Content-Transfer-Encoding' => 'binary',
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment;filename=export.csv",
'Content-Length' => strlen($content),
]);
更新 2
将响应 Content-Type 更改为 text/html 会正确返回响应。
【问题讨论】:
标签: amazon-web-services http amazon-elb