nginx - client_max_body_size has no effect

前端 未结 13 1793
无人共我
无人共我 2020-11-28 01:13

nginx keeps saying client intended to send too large body. Googling and RTM pointed me to client_max_body_size. I set it to 200m in th

相关标签:
13条回答
  • 2020-11-28 01:19

    Someone correct me if this is bad, but I like to lock everything down as much as possible, and if you've only got one target for uploads (as it usually the case), then just target your changes to that one file. This works for me on the Ubuntu nginx-extras mainline 1.7+ package:

    location = /upload.php {
        client_max_body_size 102M;
        fastcgi_param PHP_VALUE "upload_max_filesize=102M \n post_max_size=102M";
        (...)
    }
    
    0 讨论(0)
  • 2020-11-28 01:20

    Following nginx documentation, you can set client_max_body_size 20m ( or any value you need ) in the following context:

    context: http, server, location
    
    0 讨论(0)
  • 2020-11-28 01:23

    I had a similar problem recently and found out, that client_max_body_size 0; can solve such an issue. This will set client_max_body_size to no limit. But the best practice is to improve your code, so there is no need to increase this limit.

    0 讨论(0)
  • 2020-11-28 01:26

    As of March 2016, I ran into this issue trying to POST json over https (from python requests, not that it matters).

    The trick is to put "client_max_body_size 200M;" in at least two places http {} and server {}:

    1. the http directory

    • Typically in /etc/nginx/nginx.conf

    2. the server directory in your vhost.

    • For Debian/Ubuntu users who installed via apt-get (and other distro package managers which install nginx with vhosts by default), thats /etc/nginx/sites-available/mysite.com, for those who do not have vhosts, it's probably your nginx.conf or in the same directory as it.

    3. the location / directory in the same place as 2.

    • You can be more specific than /, but if its not working at all, i'd recommend applying this to / and then once its working be more specific.

    Remember - if you have SSL, that will require you to set the above for the SSL server and location too, wherever that may be (ideally the same as 2.). I found that if your client tries to upload on http, and you expect them to get 301'd to https, nginx will actually drop the connection before the redirect due to the file being too large for the http server, so it has to be in both.

    Recent comments suggest that there is an issue with this on SSL with newer nginx versions, but i'm on 1.4.6 and everything is good :)

    0 讨论(0)
  • 2020-11-28 01:27

    You need to apply following changes:

    1. Update php.ini (Find right ini file from phpinfo();) and increase post_max_size and upload_max_filesize to size you want:

      sed -i "s/post_max_size =.*/post_max_size = 200M/g" /etc/php5/fpm/php.ini
      sed -i "s/upload_max_filesize =.*/upload_max_filesize = 200M/g" /etc/php5/fpm/php.ini```
      
    2. Update NginX settings for your website and add client_max_body_size value in your location, http, or server context.

      location / {
          client_max_body_size 200m;
          ...
      }
      
    3. Restart NginX and PHP-FPM:

      service nginx restart
      service php5-fpm restart
      

    NOTE: Sometime (In my case almost every time) you need to kill php-fpm process if it didn't refresh by service command properly. To do that you can get list of processes (ps -elf | grep php-fpm) and kill one by one (kill -9 12345) or use following command to do it for you:

    ps -elf | grep php-fpm | grep -v grep | awk '{ print $4 }' | xargs kill -9
    
    0 讨论(0)
  • 2020-11-28 01:27

    If you are using windows version nginx, you can try to kill all nginx process and restart it to see. I encountered same issue In my environment, but resolved it with this solution.

    0 讨论(0)
提交回复
热议问题