Fixing 413 request entity to large on laravel + nginx
After deploying my Laravel application to an Ubuntu VPS using Nginx, I encountered this error when uploading a PDF: 413 Request Entity Too Large The application was running normally, but every upload request failed instantly. The Cause The issue wasn't related to Laravel. By default, Nginx limits the maximum size of the request body. When an uploaded file exceeds that limit, Nginx rejects the…
After deploying a Laravel application to an Ubuntu VPS utilizing Nginx, one encountered a 413 Request Entity Too Large error when attempting to upload a PDF file. Prior to this issue, the application was functioning properly. The problem was not inherent to Laravel itself, but rather a limitation imposed by Nginx. By default, Nginx sets a cap on the maximum size of the request body.
When an uploaded file surpasses this limit, Nginx terminates the request before it reaches the Laravel application. To rectify the issue, one must modify the Nginx site configuration file. This is achieved by running `sudo nano /etc/nginx/sites-available/your-domain` and adding the following line within the server block: `client_max_body_size 100M;`.
For example, the server block should appear as follows: `server { listen 80; server_name example.com; root /var/www/project/public; client_max_body_size 100M; index index.php; ... }`. Subsequent to making the modification, it is prudent to verify the configuration with the command `sudo nginx -t`. Upon confirming that everything is in order, the Nginx service should be reloaded using `sudo systemctl reload nginx`.
Once these steps are completed, the upload functionality should resume working seamlessly, without necessitating any alterations to the Laravel application. In essence, when faced with a 413 Request Entity Too Large error on a Laravel application operating within the Nginx framework, it is advisable to first scrutinize the Nginx configuration.
In the author's experience, incorporating `client_max_body_size 100M;` within the server block was sufficient to resolve the issue.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.