When I’m uploading a file which size is larger than 1MB using PHP on Nginx web server, I got this error message:
413 Request Entity Too Large |
This error indicates that nginx limits file size that client can send to the server. And PHP configuration also has limit on upload file size. Therefore, I have to modify nginx and PHP configuration to fix this issue.
My server environment in this example:
- Ubuntu 16.04
- Nginx 1.10.0
- PHP 7.0 fpm
If you use other version, path to configuration files may be different from the example.
Step-by-step
- Login to the server and type this command to edit nginx.conf.
sudo nano /etc/nginx/nginx.conf
Note: You need to enter administrator password when using sudo command.
- Add the command below in http section. You can replace the number as file size limit that you want. In this example, I set it to 10M (10 Megabytes).
client_max_body_size 10M;
- Exit the editor by press Ctrl + X. You will be asked to save changes to the file, press Y.
- Press Enter to save changes to the current filename.
- Type this command to check nginx configuration file. If the syntax is ok, you can proceed to next step. Otherwise, you have to recheck the configuration file.
sudo nginx -t
- If you already modified PHP configuration, skip to step 9.
Modify PHP configuration. You can check location of php.ini using phpinfo(). Then, edit php.ini by type this command.sudo nano /etc/php/7.0/fpm/php.ini
- Change post_max_size to 10M
- Change upload_max_filesize to 10M and save the file.
- Restart nginx and php services by type these command:
sudo service nginx restart sudo service php7.0-fpm restart
- If there is no error, your PHP configuration should be updated as in the figure below.
- Now I can upload a large file using PHP on the server.
Thanks helped me out a lot!