Nginx 报错 413 client intended to send too large body 处理
promtail通过nginx向loki发送数据,收到以下报错
level=error ts=2025-05-08T01:21:05.955472787Z caller=client.go:380 component=client host=local-loki:3100 msg="final error sending batch" status=413 error="server returned HTTP status 413 Request Entity Too Large (413): <html>"排查nginx日志,发现
2025/05/08 09:21:06 [error] 21#21: *62769236 client intended to send too large body: 1312028 bytes, client: 10.40.30.155, server: , request: "POST /loki/api/v1/push HTTP/1.1", host: "local-loki:3100"
- | 10.40.30.155 | - | 08/May/2025:09:21:06 +0800 | POST /loki/api/v1/push HTTP/1.1 | 413 | 183 | - | promtail/2.6.1 | - | 0.032 | - | - | - | local-loki:3100触发了Nginx的最大请求体限制,收到了 413 (Request Entity Too Large) error
原因:
Nginx 默认的 client_max_body_size 是 1MB,而你这里的请求是 ~1.25MB,所以触发了限制。
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location设置客户端请求正文允许的最大大小。如果请求的大小超过了配置值,就会向客户端返回 413(Request Entity Too Large(请求实体过大))错误。请注意,浏览器无法正确显示此错误。将 size 设置为 0 将禁止检查客户端请求正文的大小。
解决方法:
如果设置为0,可以上传任意大小,但是存在被滥用的风险(如Dos攻击),可以根据实际日志大小,设置一个合理的上限。
这里通过查看nginx错误日志,获取到请求体最大为 2675495 bytes,也就是 ~ 2.5MB
# 查看413错误日志中最大请求体
docker logs logging-loki-gateway | \
grep "client intended to send too large body" | \
cut -d',' -f1|cut -d':' -f 5 | \
awk 'BEGIN{a=0}{if($1>a) a=$1 fi}END{print a}'
# output
2675495调整nginx中的 client_max_body_size 值,设置为5M
只针对上传接口调整
通过反向代理访问,可以在对应的server或者location块中添加
location = /loki/api/v1/push {
client_max_body_size 5m;
proxy_pass http://write$request_uri;
}全局修改
如果全局修改,需要在nginx.conf的http块中添加或修改:
http {
client_max_body_size 5M;
...
}