经常我们会遇到上传超过1M左右的文件,但是向K8S中的Ingress nginx发起请求的时候会报错413 Request Entity Too Large。
具体原因是因为ingress nginx 默认 client_max_body_size为 1M, 超过 1M 后会报错 413 Request Entity Too Large。
解决办法
找到k8s集群 的ingress路由规则配置,编辑yaml,添加如下注解
nginx.ingress.kubernetes.io/proxy-body-size: 20m
我这里是springcloud微服务,所有的请求都会经过springcloud gateway网关服务,因此,我只需要在gateway网关服务的Ingress路由规则中添加注解设置为最大20M即可。示例如下:
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: ingress-gateway
namespace: c2c-dev
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/cors-allow-credentials: 'true'
nginx.ingress.kubernetes.io/cors-allow-methods: 'GET, PUT, POST, DELETE, PATCH, OPTIONS'
nginx.ingress.kubernetes.io/cors-allow-origin: '*'
nginx.ingress.kubernetes.io/enable-cors: 'false'
nginx.ingress.kubernetes.io/proxy-body-size: 20m
spec:
rules:
- host: gateway.dev.c2c.inc
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: kkb-gateway
port:
number: 80