Nginx – Php ignores file upload limits

nginxPHPphp-fpm

I am running a bookstack instance in php under nginx.

Unfortunately i can't upload any file bigger than 2M. And this is my problem for today.

File upload attempt results in HTTP/1.1 422 Unprocessable Entity
with body {file: ["The file could not be uploaded. The server may not accept files of this size."]}.

Bookstack documentation says it's enough to set up file size limits in both nginx and php (actually it also mentions some tweak in frontend, but since we have HTTP error let's leave it out of scope). No tweaks in the bookstack backend required.

php config (and there is only one version installed):

cat /etc/php/7.0/fpm/php.ini | egrep 'upload_max_filesize|post_max_size'

post_max_size = 28M  
upload_max_filesize = 28M

nginx config:

cat /etc/nginx/sites-available/bookstack

server {  
    listen      80;
    server_name ****;
    return 301 https://****;
}  

server {  
    listen               443 ssl;
    server_name          ****;
    ssl_certificate      /etc/ssl/certs/***.crt;
    ssl_certificate_key  /etc/ssl/private/***.key;
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers          HIGH:!aNULL:!MD5;

    root ***;
    client_max_body_size 28m;
    client_body_timeout 60;

    location / {
        try_files $uri /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

I've added this file into public directory:

<?php
echo phpinfo();
?>

Which gives:

PHP Version 7.0.33-0ubuntu0.16.04.7

Server API FPM/FastCGI

Configuration File (php.ini) Path /etc/php/7.0/fpm
Loaded Configuration File /etc/php/7.0/fpm/php.ini

post_max_size 16M
upload_max_filesize 2M

So, it uses the correct php.ini file but ignores its content?

Now, the FUN part!

I've also found that configuration is correct just after sudo service php7.0-fpm restart:

post_max_size 28M
upload_max_filesize 28M

…but for a couple of seconds only. After one or two seconds it resets back to 2M. And file upload actually works within these seconds (but requires a lot of dexterity…).

According to service php7.0-fpm status output, main service process does not restart within this period, and worker process PID's remain unchanged.

Also, enabling catch_workers_output = yes in www.conf does not add anything useful to the fpm log.

How can i extend file upload limit for longer period than two seconds?
I guess i could just restart php-fpm every two seconds but this is not even funny, actually.
Is there more robust way to solve the problem?

Best Answer

I have 2 bookstack instances running, one on apache and one on nginx, both on php 7.2. Strangely, my nginx one is fine, but today I discovered that my apache instance has this error as well.

I managed to fix the error by copying my settings (post_max_size and upload_max_filesize, 200M for both) from /etc/php/7.2/fpm/php.ini to /etc/php/7.2/apache2/php.ini. Since you're running on nginx, I'm not sure if this would help you, but it's something to try, at least.

Related Topic