PHP和Nginx 文件上传大小限制问题解决方法

对于nginx+php的一些网站,上传文件大小会受到多个方面的限制,一个是nginx本身的限制,限制了客户端上传文件的大小,一个是php.ini文件中默认了多个地方的设置。

所以为了解决上传文件大小限定的问题必须要做出多处修改。以下整理了几个地方。

1、修改/usr/local/nginx/conf/nginx.conf 文件,查找 client_max_body_size 将后面的值设置为你想设置的值。比如:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
            #  
            location ~ \.php$ {  
                root           /home/www/htdocs;  
                fastcgi_pass   127.0.0.1:9000;  
                fastcgi_index index.php;  
                fastcgi_param SCRIPT_FILENAME /home/www/htdocs$fastcgi_script_name;  
                include        fastcgi_params;  
      
                client_max_body_size 35m;        #客户端上传文件大小设为35M  
                client_body_temp_path /home/www/nginx_temp;        #设置临时目录  
            }

附录:Nginx有一个Upload组件:
上传速率,上传Body大小,也就是上传文件时可能较大?
client_max_body_size 1024M
upload_limit_rate 158k
如下:

location /upload {
            upload_pass     /up.php;
            upload_cleanup 400 404 499 500-505;
            #upload_store    /data/app/test.local/upload_tmp;
            upload_store    /tmp;
            upload_store_access user:r;
            client_max_body_size 1024M;
            upload_limit_rate 158k;
            upload_set_form_field "${upload_field_name}_name" $upload_file_name;
            upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
            upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
            upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
            upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
            upload_pass_form_field "^.*$";
            #upload_pass_form_field "^pid$|^tags$|^categoryid$|^title$|^userid$|^user_id$|^is_original$|^upload_file_name$|^upload_file_content_type$|^upload_file_path$|^upload_file_md5$|^upload_file_size$";
        }

2、修改php.ini

upload_max_filesize = 8M   
post_max_size = 10M   
memory_limit = 20M  
max_execution_time=300  
file_uploads = On  #默认允许HTTP文件上传,此选项不能设置为OFF。
upload_tmp_dir =/tmp/www

在上传大文件时,你会有上传速度慢的感觉,当超过一定的时间,会报脚本执行超过30秒的错误,这是因为在php.ini配置文件中 max_execution_time配置选项在作怪,其表示每个脚本最大允许执行时间(秒),0 表示没有限制。你可以适当调整max_execution_time的值,不推荐设定为0。

上一篇:C# 泛型类和泛型方法


下一篇:[django]前后端分离之JWT用户认证