Kubernetes与Nginx Ingress Controller 413错误配置调整
Hey there, I see you're battling the HTTP 413 error with your Nginx Ingress—totally frustrating when you've tweaked configs but still hit that wall. Let's break down what's going on here and get this sorted.
1. Fix Your ConfigMap First
Looking at your existing ConfigMap, you've got both proxy-body-size and body-size set, but here's the key issue: body-size isn't a valid parameter for the Nginx Ingress Controller. The only parameter that controls the global client_max_body_size setting is proxy-body-size. Let's clean that up to set your desired 64m limit:
kind: ConfigMap apiVersion: v1 data: proxy-connect-timeout: "15" proxy-read-timeout: "600" proxy-send-timeout: "600" # This is the correct parameter to control client_max_body_size globally proxy-body-size: "64m" hsts-include-subdomains: "false" server-name-hash-bucket-size: "256" metadata: name: nginx-ingress-controller namespace: kube-system # Adjust this to match your Ingress Controller's namespace
2. Make the Config Stick
Changing the ConfigMap alone won't do it—you need to restart the Nginx Ingress Controller pods so they pick up the new settings. A rolling restart is the safest way to avoid downtime:
# Swap in your controller's deployment name and namespace kubectl rollout restart deployment nginx-ingress-controller -n kube-system
3. Double-Check It Worked
Don't just take my word for it—verify the config is actually applied. Jump into one of the controller pods and check the Nginx config directly:
# Replace the pod name and namespace with your own kubectl exec -it nginx-ingress-controller-abc123-xyz789 -n kube-system -- /bin/sh # Search for the client_max_body_size setting grep -r "client_max_body_size" /etc/nginx/
You should see output that includes client_max_body_size 64m;—that means it's working as intended.
4. Override for Specific Ingresses (If Needed)
If you have one particular Ingress that needs a different limit (say, 128m instead of 64m), you can add an annotation directly to that Ingress resource. This will override the global ConfigMap setting for that route:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: your-custom-ingress annotations: nginx.ingress.kubernetes.io/proxy-body-size: "128m" # Per-ingress override spec: # Your existing Ingress rules go here
5. Watch Out for These Common Mistakes
- Wrong Namespace: Make sure your ConfigMap is in the same namespace as your Nginx Ingress Controller—if they're in different namespaces, the controller will never see the config.
- Forgot to Restart Pods: Old pods will keep using the old config until you restart them—don't skip the rolling restart step!
- Typos in Parameter Names: Using underscores (
proxy_body_size) instead of hyphens (proxy-body-size) will cause the controller to ignore the setting—stick to hyphens.
内容的提问来源于stack exchange,提问作者Djent

