Setting up kubernets Ingress proxy-body-size based on request method

1/11/2022

I've been trying to set up a max body size in the Ingress controller based on the HTTP method of a given path.

Basically the POST method should allow 3m as max size and all the other methods should allow 1m.

Right now my main idea was to do something like:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-custom-service
  namespace: development
  annotations:
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "false"
    kubernetes.io/ingress.class: "nginx-dev"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      internal;
      rewrite ^ $original_uri break;
    nginx.ingress.kubernetes.io/server-snippet: |
      location /api/v1/my-endpoint {
        if ( $request_method = POST) {
          set $target_destination '/_post';
          client_max_body_size 3M;
        }
        if ( $request_method != POST) {
          set $target_destination '/_not_post';
          client_max_body_size 1M;
        }
        set $original_uri $uri;
        rewrite ^ $target_destination last;
      }

spec:
  tls:
  rules:
  - host: my-host.com
    http:
      paths:
        - path: /_post
          backend:
            serviceName: my-service
            servicePort: 8080
        - path: /_not_post
          backend:
            serviceName: my-service
            servicePort: 8080

But then I'm getting the following error in the pod: "client_max_body_size" directive is not allowed here in /tmp/nginx-cfg857596568:3187

Is there any way I can correctly set-up the max body size via the ingress controller?

-- Alexandre Xavier
kubernetes
kubernetes-ingress

1 Answer

1/11/2022

Try changing your annotations with the configuration-snippet

nginx.ingress.kubernetes.io/configuration-snippet: |

      location /upload-path {

           client_max_body_size 8M;  
      }

Read more at : https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md#configuration-snippet

-- Harsh Manvar
Source: StackOverflow