Error 413 Request Entity Too Large 处理方式

背景

今天公司某个服务接口在使用过程中出现 413 Request Entity Too Large 错误。

原因

经过排查发现是由于在调用该接口(post 请求)是发送的数据量超过 nginx 默认数据上传大小,查看 nginx 官网可以看到如下解释:

syntax: client_max_body_size size

default: client_max_body_size 1m

context: http, server, location

Directive assigns the maximum accepted body size of client request, indicated by the line
Content-Length
in the header of request.

If size is greater the given one, then the client gets the error "Request Entity Too Large" (413).

It is necessary to keep in mind that the browsers do not know how to correctly show this error.

nginx 默认配置 client_max_body_size:1m

解决方式

  • 直接部署在服务器上的服务
    如果是直接手动部署在服务器上的服务可以通过修改 nginx 中的配置来修改 nginx 默认值,具体可以进入 /etc/nginxnginx.conf中在对应的 server 中加入 client_max_body_size 修改后的值即可
server {
        listen 80;
        server_name example.com;
        location / {
            root   html;
            index  index.html index.htm;
            client_max_body_size  25m; # 加入修改后你需要的大小
        }
    }

保存并重启 nginx: nginx -s reload

  • 如果服务是部署在 kubernetes 中且服务是通过 ingress 对外暴露服务需要在 ingress 中加入如下配置
# ingress
ingress:
  enabled: true
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "25m"	# 加入该配置
  hosts:
    - example.com
  paths:
    - path: /
      servicePort: 80
  tls: true

参考

nginx 官方文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值