# 大文件与长请求：IIS、Nginx、应用三层里的 413/504

> 在每一跳调整 body 限制、超时和缓冲，并选择流式/异步而不是 30 分钟 HTTP 请求。

**测试环境**

- 版本号: 26100.33650
- 版本: Standard / Datacenter
- 架构: x64
- 证据: E1
- 核验日期: 2026-10-29

## 一句话结论

一次大上传跨多个限制：

| 跳 | 限制 |
|---|---|
| 浏览器/应用 | 自己的 body 上限 |
| IIS | `maxAllowedContentLength`（字节） |
| Nginx | `client_max_body_size` |
| 代理 | ARR/proxy body 限制 |
| 应用 | 框架 body 限制 |
| 时间 | HTTP.sys `requestTimeout`、Nginx `client_body_timeout`、代理读超时 |

先加最小的那个，不要全部设无限。

## IIS

```xml
<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="524288000" />
    </requestFiltering>
  </security>
</system.webServer>
```

`524288000` = 500MB。还要查处理器/应用限制（ASP.NET `maxRequestLength`、ANCM body 限制）和 `serverRuntime requestTimeout`。

上传时 `uploadReadAheadSize` 可能影响客户端证书/大请求头；不要盲目加大。

## Nginx

```nginx
client_max_body_size 500m;
client_body_timeout 300s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
```

`client_body_temp_path` 要有磁盘空间。大文件流式上传可 `proxy_request_buffering off`，直接传给应用（代价：应用要处理慢客户端）。

## 应用

框架限制也要改：

- ASP.NET Core：`RequestSizeLimit`/Kestrel `MaxRequestBodySize`；
- Express：`express.json({ limit: '...' })` / multer；
- Flask：`MAX_CONTENT_LENGTH`；
- Tomcat：`maxPostSize`/connector。

应用限制更低，代理放行后应用仍会 413。

## 大文件用流式

2GB 上传或 30 分钟导出不该是一个同步 HTTP 请求：

- 对象存储预签名直传；
- 分片/可续传；
- 导出走后台任务 + 状态端点；
- 下载用签名 URL/流，超时有界。

> **警告: 超时不是慢任务的解药**
>
> 把 `requestTimeout` 设几小时会占住 worker/连接，部署重启都痛苦。长任务进队列，返回 job id。

## 验证

```powershell
curl.exe -I -H "Content-Length: 600000000" https://lab.example/upload
curl.exe --limit-rate 100k -T big.bin https://lab.example/upload
```

看每一跳状态和 IIS/Nginx 日志。`413` 是 body 限制；`504` 是超时；`502` 是上游。

## 实验室边界

E1。实验室没有把大上传推过 IIS/Nginx；限制是 Server 2025 的跨跳检查表。

## 来源

- [Microsoft Learn — 请求限制](https://learn.microsoft.com/zh-cn/iis/configuration/system.webserver/security/requestfiltering/requestlimits/)
- [nginx — core 模块](https://nginx.org/en/docs/http/ngx_http_core_module.html)
- [nginx — proxy 模块](https://nginx.org/en/docs/http/ngx_http_proxy_module.html)

---

来源页面: https://winbook.fyi/zh/server/tls/large-files/
