解决 Nginx 报错 413 Request Entity Too Large 文件过大
分析 Nginx 上传附件报 client intended to send too large body 根因,详解 client_max_body_size 调优与 Ingress 批处理参数。
问题现象
前端用户在网站上传大图片、视频或批量导入 Excel 数据时,请求立即被中止并弹出提示:
HTTP/1.1 413 Request Entity Too Large
Server: nginx/1.24.0
Content-Type: text/html
Content-Length: 183
<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>
查看 Nginx error.log:
2026/11/15 09:40:00 [error] 19283#0: *120 client intended to send too large body: 15420310 bytes, client: 192.168.1.100, request: "POST /api/upload HTTP/1.1"
触发根因
- 默认
client_max_body_size限制过小:Nginx 默认配置的客户端最大请求体为1m(1MB)。当上传的文件或 POST 请求体超过 1MB 时直接由 Nginx 核心拒绝。 - Kubernetes Ingress 注解缺失:在使用 Nginx Ingress 时,默认同样为 1MB,未在 Ingress YAML 中声明
proxy-body-size。
诊断排查步骤
- 检查当前 Nginx 配置文件中各层级(http / server / location)对该参数的声明:
grep -rn "client_max_body_size" /etc/nginx/
解决步骤与操作命令
步骤一:针对传统 Nginx 主机调整配置
在 nginx.conf 对应的 http、server 或具体的上传 location 块中提高上限(例如调至 100MB 或设为 0 彻底关闭限制):
http {
# 全局生效
client_max_body_size 100M;
server {
listen 80;
server_name example.com;
location /api/upload {
# 仅对上传路径生效
client_max_body_size 500M;
client_body_buffer_size 128k;
proxy_pass http://backend_upload;
}
}
}
步骤二:针对 Kubernetes Ingress 补充注解
若通过 Kubernetes Nginx Ingress Controller 代理,在 Ingress 资源中增加 Annotation:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: upload-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/proxy-body-size: "500m"
nginx.ingress.kubernetes.io/client-body-buffer-size: "1m"
步骤三:平滑重载 Nginx
nginx -t && systemctl reload nginx
验证方法
使用 curl 发起大文件 POST 传输测试:
curl -F "file=@large_video.mp4" -X POST http://example.com/api/upload -w "%{http_code}
"
响应状态码成功返回 HTTP 200,Nginx error.log 中不再出现 client intended to send too large body。


