nginx + ingress + gunicorn 环境上传大文件报错问题的解决思路
在基于 Kubernetes 部署,使用 Gunicorn 运行的 Python Web 应用中,上传大文件时出现了一系列的错误,现在将解决问题的思路记录如下。 文件上传过程 上传文件流程 上传的文件首先到达 Kubernetes 所在的宿主机。 宿主机上的 Nginx 通过 Proxy 转发给 Kubernetes 集群中的 Ingress Controller,Ingress controller 也是使用 Nginx 实现的。 Ingress Controller 中的 Nginx 通过 Proxy 转发给 Gunicorn。 Gunicorn 会启动若干个 Worker 处理请求,所以 Gunicorn 会再转发给 Worker。 Worker 就是最终的 Python Web App 错误 413 的解决 首先碰到的是 413 Request Entity Too Large 错误,在上传过程中连接被中断(基本上每次都是相同的上传百分比被中断),请求返回 413,首先考虑到 Nginx 对于请求体的大小有限制,查看 Nginx 文档,发现 client_max_body_size 参数控制请求体的大小,默认的设置是 1mb。 client_max_body_size: Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size. ...