我有一个带有目标组的 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 字节后,响应都会被截断。
有任何想法吗?
UPDATE
如果我省略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),
]);
UPDATE 2
更改响应Content-Type
to be text/html
正确返回响应。