Nginx + PHP 设置文件上传大小

你可能会碰到服务器 413 错误,当文件上传到服务器时,返回如下结果:

HTTP/1.1 413 Request Entity Too Large

原因

服务器限制了文件上传大小。默认大小,Nginx 为1M,PHP 为2M。因此超过1M便返回413.

如何修改大小限制

需要同时修改 PHP 和 Nginx 的配置。

PHP

编辑 php.ini 文件,找到对应的位置:

; Maximum size of POST data that PHP will accept.
; http://php.net/post-max-size
post_max_size = 10M

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 8M

post_max_size 指表单 POST 数据的最大值;
upload_max_filesize 指上传文件的最大值;
注意:这个参数 file_uploads = On,确保它是打开的。

Nginx

编辑 nginx.conf 文件或者子配置文件,在 server 段的 location 中配置:

location / {
    root   /home/eric/workspace/GameFeedback/www;
    index  index.html index.htm index.php;
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php?/$1 last;
    }
    client_max_body_size 8m;
}

location ~ \.php$ {
    root           /home/eric/workspace/GameFeedback/www;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
    client_max_body_size 8m;
}

注意:两个 location 都要配置,否则无效。

最后,需要重启 Nginx 服务;如果是 php-fpm,也要重启以使配置生效。

标签: php, nginx

添加新评论