188

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?

4 Answers 4

332

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.

10
  • 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 3, 2014 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 3, 2014 at 15:38
  • 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 Nov 3, 2014 at 15:57
  • 5
    sudo nginx -s reload reloads configuration without restarting nginx
    – Vincnetas
    Nov 28, 2017 at 9:43
  • 2
    php needs post_max_size as well.
    – logicbloke
    Jul 14, 2020 at 13:49
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.

1
  • 1
    This related project: github.com/evertramos/… (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, 2020 at 9: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.

2
  • 5
    This question is about the nginx web server, not the PHP programming language
    – Torque
    Nov 2, 2020 at 13:47
  • 1
    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 3, 2020 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

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.