Nginx一直说客户端打算发送太大的身体。谷歌和RTM让我找到了client_max_body_size。我在Nginx .conf和vhost conf中设置了200m,重启了Nginx几次,但我仍然得到错误消息。

我是不是忽略了什么?后端是php-fpm (max_post_size和max_upload_file_size是相应设置的)。


当前回答

您需要应用以下更改:

Update php.ini (Find right ini file from phpinfo();) and increase post_max_size and upload_max_filesize to size you want: sed -i "s/post_max_size =.*/post_max_size = 200M/g" /etc/php5/fpm/php.ini sed -i "s/upload_max_filesize =.*/upload_max_filesize = 200M/g" /etc/php5/fpm/php.ini``` Update NginX settings for your website and add client_max_body_size value in your location, http, or server context. location / { client_max_body_size 200m; ... } Restart NginX and PHP-FPM: service nginx restart service php5-fpm restart

注意:有时(在我的情况下几乎每次),如果php-fpm进程没有通过service命令正确刷新,你需要杀死它。要做到这一点,你可以获得进程列表(ps -elf | grep php-fpm)并逐个杀死(kill -9 12345)或使用以下命令为你做:

ps -elf | grep php-fpm | grep -v grep | awk '{ print $4 }' | xargs kill -9

其他回答

我遇到了同样的问题,但我发现它与nginx无关。我使用nodejs作为后端服务器,使用nginx作为反向代理,413代码是由节点服务器触发的。节点使用koa解析正文。Koa限制url编码的长度。

formLimit: url编码体的限制。如果主体最终大于此限制,则返回413错误代码。默认为56kb。

将formLimit设置为更大可以解决这个问题。

如果你尝试了上面的选项,没有成功,你也使用IIS (iisnode)托管你的节点应用程序,把这段代码放在web上。Config帮我解决了这个问题:

这里是参考资料:https://www.inflectra.com/support/knowledgebase/kb306.aspx

此外,您可以更改允许的长度,因为现在我认为它是2GB。根据你的需要修改它。

  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>

我最近有一个类似的问题,发现client_max_body_size 0;可以解决这样的问题。这将把client_max_body_size设置为无限制。但最佳实践是改进您的代码,因此没有必要增加这个限制。

请查看是否在http{}块中设置client_max_body_size指令,而不是在location{}块中设置client_max_body_size指令。我已经在http{}块内设置了它,它可以工作

如果您正在使用Kubernetes,请在Ingress中添加以下注释:

annotations:
  nginx.ingress.kubernetes.io/client-max-body-size: "5m"
  nginx.ingress.kubernetes.io/client-body-buffer-size: "8k"
  nginx.ingress.kubernetes.io/proxy-body-size: "5m"
  nginx.ingress.kubernetes.io/proxy-buffer-size: "8k"

确认已应用更改:

kubectl -n <namespace> describe ingress <ingress-name>

引用:

客户端缓冲区大小 自定义最大身体尺寸