在开发音乐站,需要上传音乐文件。普通 MP3 文件一般都只有几兆,问题不大;无损文件基本都到 30M~100M 了,超过默认最大上传文件限制,这样就返回了 413 Request Entity Too Large ,我们使用的技术栈是 Nginx + Laravel,需要相应调整。
默认配置文件路径 /etc/ngin/nginx.conf ,在 http、server 或 location 块中添加:
client_max_body_size 100M; # 设置为需要的值,如 100M, 1G 等示例:
server {
listen 80;
server_name example.com;
client_max_body_size 100M;
location /upload {
client_max_body_size 500M; # 可以针对特定路径设置
proxy_pass http://backend;
}
}client_max_body_size 为 0 时表示不限制大小。
默认配置文件路径 /etc/php/8.4/fpm/php.ini ,相关的配置项有 upload_max_filesize ,post_max_size 。
示例:
upload_max_filesize = 100M
post_max_size = 100M如果上传的文件比较大,上传带宽不大,可能会引起超时以及超内存,还需要调整如下配置:
max_execution_time = 300
max_input_time = 300
memory_limit = 256M调整配置后,重启服务问题解决。