大网数据广告活动
服务器运维问答

How to configure client_max_body_size per a location using nginx controller

· 52CE 我爱测

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 wan…

问题原文

原帖链接 · Peter Jurkovič · CC BY-SA 4.0

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;
}

社区高分解答

原帖链接 · PjoterS · CC BY-SA 4.0

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;  
      }

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-04-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。