185

I want to increase the maximum file size that can be uploaded.
After doing some research online, I found that you have to edit the file 'nginx.conf'.
The only way I can currently access this file is by going through Putty and typing in the command:

vi /etc/nginx/nginx.conf

This will open the file but I have 2 questions now:

  1. How do I edit this file?
  2. I found online that you have to add the following line of code:

client_max_body_size 8M;

Where would I put this line of code in nginx.conf?

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
Jeff P.
  • 2,754
  • 5
  • 19
  • 41

4 Answers4

323

Add client_max_body_size

Now that you are editing the file you need to add the line into the server block, like so;

server {
    client_max_body_size 8M;

    //other lines...
}

If you are hosting multiple sites add it to the http context like so;

http {
    client_max_body_size 8M;

    //other lines...
}

And also update the upload_max_filesize in your php.ini file so that you can upload files of the same size.

Saving in Vi

Once you are done you need to save, this can be done in vi with pressing esc key and typing :wq and returning.

Restarting Nginx and PHP

Now you need to restart nginx and php to reload the configs. This can be done using the following commands;

sudo service nginx restart
sudo service php5-fpm restart

Or whatever your php service is called.

Community
  • 1
  • 1
Matt Burrow
  • 10,477
  • 2
  • 34
  • 38
  • 10
    Might not be a bad idea to add it to `http {` context so that all servers inherit it. And make sure that you adjust your `upload_max_filesize` in `php.ini` to match your nginx setting. If you are running Laravel using Homestead/Vagrant, that file is located at `/etc/php5/fpm/php.ini`. – damiani Nov 03 '14 at 15:31
  • Yes I forgot to mention that I am using Laravel Homestead. So I opened '/etc/php5/fpm/php.ini' and found the line of code 'upload_max_filesize = 2M', so I want to change it so that '2M' becomes '8M'. Once I make this edit, do I just hit 'Ctrl Z' to exit out of the file and then run 'sudo service nging restart'? – Jeff P. Nov 03 '14 at 15:38
  • You dont hit ctrl-z You need to save it with vi press esc then type :wq and press enter. – Matt Burrow Nov 03 '14 at 15:39
  • I'm trying to use the command ':wq!' after making the edit but I keep getting the message 'E212 Can't open file for writing'. How do I fix this? – Jeff P. Nov 03 '14 at 15:51
  • 2
    Come out of the editor, prepend the vi command with sudo. Like so; sudo vi /etc/nginx/nginx.conf or sudo vi /etc/php5/fpm/php.ini – Matt Burrow Nov 03 '14 at 15:57
  • Yes that worked perfectly! I had to use the 'sudo' command in front of vi so that it granted it admin access and allowed for the files to be edited. After making your changes, I ran the 'sudo service' commands listed above and now I can upload images with large file sizes. Thank you so much!!! – Jeff P. Nov 03 '14 at 16:06
  • 5
    `sudo nginx -s reload` reloads configuration without restarting nginx – Vincnetas Nov 28 '17 at 09:43
  • 2
    php needs `post_max_size` as well. – logicbloke Jul 14 '20 at 13:49
  • You can also reload nginx config using this command: `service nginx reload` – michal-michalak Oct 30 '20 at 14:24
  • what is 8M? does it mean 8 MB size? – Conor Sep 10 '22 at 14:31
16

In case if one is using nginx proxy as a docker container (e.g. jwilder/nginx-proxy), there is the following way to configure client_max_body_size (or other properties):

  1. Create a custom config file e.g. /etc/nginx/proxy.conf with a right value for this property
  2. When running a container, add it as a volume e.g. -v /etc/nginx/proxy.conf:/etc/nginx/conf.d/my_proxy.conf:ro

Personally found this way rather convenient as there's no need to build a custom container to change configs. I'm not affiliated with jwilder/nginx-proxy, was just using it in my project, and the way described above helped me. Hope it helps someone else, too.

Vladimir Salin
  • 2,951
  • 2
  • 36
  • 49
  • 1
    This related project: https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion (the nginx-proxy from jwilder combined with certificate generation from lets encrypt) has a neat script to do this and has this option preconfigured (to 100M). Create the .env from the sample; uncomment the line ```#USE_NGINX_CONF_FILES=true```; run ```start.sh``` and finally ```docker-compose up```. (I'm not affiliated with either project). – glaux May 28 '20 at 09:17
12

First Navigate the Path of php.ini

sudo vi /etc/php/7.2/fpm/php.ini

then, next change

upload_max_filesize = 999M
post_max_size = 999M

then ESC-->:wq

Now Lastly Paste this command,

sudo systemctl restart php7.2-fpm.service

you are done.

Rafik Farhad
  • 1,182
  • 2
  • 12
  • 21
Vasu
  • 515
  • 5
  • 7
  • 4
    This question is about the nginx web server, not the PHP programming language – Torque Nov 02 '20 at 13:47
  • This is worked for me @Rafik Farhad. It might help other too. I know it is an nginx configuration question. But In some cases, it might be required to add some other functionality as well. So I did here. Thanks. – Vasu Nov 03 '20 at 16:48
3

You can increase client_max_body_size and upload_max_filesize + post_max_size all day long. Without adjusting HTTP timeout it will never work.

//You need to adjust this, and probably on PHP side also. client_body_timeout 2min // 1GB fileupload

Digital Human
  • 1,599
  • 1
  • 16
  • 26
  • More information about tweaking your web-server here: https://doc.owncloud.com/server/admin_manual/configuration/files/big_file_upload_configuration.html – Digital Human Sep 10 '19 at 10:40