How to configure client_max_body_size per a location using nginx controller

5,715

As per Nginx docs, you can set client_max_body_size in 3 sections:

  • http,
  • server,
  • location

Do set this value, you must change it in nginx ingress controller pod, exactly in /etc/nginx/nginx.conf.

Below example:

$ kubectl exec -ti <ingres-controller-pod> /bin/bash

$ kubectl exec -ti nginx-ingress-controller-6b85b64f49-rwxlf /bin/bash

Edit nginx.conf file.

$ vi /etc/nginx/nginx.conf

In my example hostname from ingress is my.pod.svc.

Now you need to find proper server part of file. You can search it, as it will be commented like below.

## start server <your host name from ingress>

Like below:

 ## start server my.pod.svc                    
        server {                                                   
                server_name my.pod.svc ;   
...

Now you must find proper location. In this example case it will be /pod.

 location ~* "^/pod" {                                                                                                          

                        set $namespace      "default"; 

Here you must specify this value.

As default it is 2M. During change, don't forget about ;.

There is similar thread on Stackoverflow. In that thread you can also find link with another example here.

After this change you will need to reload nginx.

EDIT:

Another option (which was used by OP in this situation) is to use annotation.

client_body_max_size

nginx.ingress.kubernetes.io/proxy-body-size: 8m

This way it would apply whole cluster.

another annotation that can be use is configuration_snippet annotation. For this example, set size only to /upload-path, it would looks like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
   name: nginx-snippet
   annotations:
      nginx.ingress.kubernetes.io/configuration-snippet: |

      location /upload-path {

           client_max_body_size 8M;  
      }
Share:
5,715

Related videos on Youtube

Peter Jurkovič
Author by

Peter Jurkovič

Freelance web developer

Updated on September 18, 2022

Comments

  • Peter Jurkovič
    Peter Jurkovič almost 2 years

    I would need to configure client_max_body_size for a specific location in my Ingress configuration file then is the default value. How it is possible to do it? I was looking into the doc but have not found anything. Not don't want do do it globally.

    Also if I would use an annotation it would be used for all paths, I just want it for a specific one.

      annotations:
        nginx.ingress.kubernetes.io/proxy-body-size: "4m"
    
    location /upload-path {
      client_max_body_size 6M;
    }
    
  • Peter Jurkovič
    Peter Jurkovič about 4 years
    Hey, thanks. I've decided to do in on an ingress rule level - on in the controller.
  • PjoterS
    PjoterS about 4 years
    So have you used annotation nginx.ingress.kubernetes.io/proxy-body-size or configuration-snippet like here: stackoverflow.com/a/59212766/11148139 ?
  • Peter Jurkovič
    Peter Jurkovič about 4 years
    I have used the annotation and created a new ingress rule specifically for the path needed.