一、关于nginx中控制文件上传大小的配置项
在文件上传时,如果上传文件比较大会出现报错:Status Code: 413 Request Entity Too Large,此时的响应头信息详细示例如下:
Request Method: POST
Status Code: 413 Request Entity Too Large
Remote Address: 10.147.26.139:80
Referrer Policy: no-referrer-when-downgrade
这正是超过了nginx配置的上传文件大小限制导致的。配置项即是client_max_body_size size,其默认大小为1M,详细文档说明:
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.
在视频上传网站,此项配置我们可以设置为1G以上,按需配置:
client_max_body_size 2048M;
可以选择在http{ }/server{ }/location{ }中设置此项, 控制的范围不同。另外修改此项配置必须杀掉nginx重新启动,否则不能生效。这个是正常的,有些核心参数的修改必须杀掉重启。另外在网上看到一个说法:说用nginx来做webserver的时这种中断在nginx的请求中是无法记录到访问的,其实会记日志:包括 nginx proxy 以及nginx web上都有413错误记录:
2019/11/09 15:28:06 [error] 2705932#0: *3448 client intended to send too large body: 302491285 bytes, POST /uploadfile?fileName
2019/11/09 15:28:06 [error] 8#8: *4949 client intended to send too large body: 302491285 bytes, client: 15.17.2.19
最后,虽然nginx上的配置修改了,但是nginx后端的服务也需要进行相应修改,比如后端php的话就需要修改 post_max_size,memory_limit,upload_max_filesize 三项配置值:建议memory_limit的值比上传文件的大小值大点,因为memory_limit除了上传的文件外还有其它程序等所占用空间,不然实际可上传的文件大小肯定不到2048M。
post_max_size = 2048M
memory_limit = 2248M
upload_max_filesize = 2048M
二、磁盘满导致APP接口上传文件返回500
APP接口那边反馈说一上传文件就出问题,但所有文件上传的接口我之前都调试了,却没有发现问题,试了一下APP接口,一上传文件就返回500。不经意间发现服务器的磁盘满100%了(/dev/xvda100%):
[root@AY /]# df -hl
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 20G 20G 0 100% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/xvdb1 227G 7.1G 208G 4% /home
我擦,真不知道是什么情况啊,使用命令:du -sh ./* 从顶层开始排查文件夹的大小,发现mysql的文件大小异常啊,这是已经删除后的情况了,之前这种1个多G的文件有快10个了,整个MYSQL占了13G的空间。
这下总算找到原因了,上回我准备弄主丛同步,把这个服务器当主服务器,后来没有弄同步也忘记关闭主服务器的设置了,结果就导致这个文件一直累加啊。
mysql的此项配置在配置文件中:log-bin=mysql-bin //[不是必须]启用二进制日志(如采用文件日志同步,必须打开这项),重新启动MYSQL即可:
/mysqld --defaults-file=/etc/my.cnf --user=root
publish:August 17, 2015 -Monday