How to edit nginx.conf to increase file size upload

NginxNginx Config

Nginx Problem Overview


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?

Nginx Solutions


Solution 1 - Nginx

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.

Solution 2 - Nginx

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.

Solution 3 - Nginx

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.

Solution 4 - Nginx

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff P.View Question on Stackoverflow
Solution 1 - NginxMatt BurrowView Answer on Stackoverflow
Solution 2 - NginxVladimir SalinView Answer on Stackoverflow
Solution 3 - NginxVasuView Answer on Stackoverflow
Solution 4 - NginxDigital HumanView Answer on Stackoverflow