How to change file upload size in nginx.conf

Change the maximum file upload size in Nginx using the client_max_body_size directive in nginx.conf to allow larger or smaller uploads.

Change the maximum file upload size on the Nginx web server by setting the client_max_body_size directive in nginx.conf.

Prerequisites

  • Root or sudo access to the server running Nginx.
  • SSH access to edit the nginx.conf configuration file.

Step-by-Step: Change File Upload Size in nginx.conf

  1. Open the nginx.conf configuration file in a text editor. The file resides at /etc/nginx/nginx.conf on most Linux distributions.
sudo vi /etc/nginx/nginx.conf
  1. Add or update the client_max_body_size directive inside the server { } block. Nginx uses this directive to set the maximum allowed size of the client request body, including file uploads. The default value is 1M (1 megabyte). Set the value to the desired limit using M for megabytes or G for gigabytes.
server {
    client_max_body_size 50M;

    # other directives
}

Nginx also accepts client_max_body_size in the http { } context to apply the limit globally across all server blocks. Place it in a location { } block to restrict the limit to a specific URL path such as an upload endpoint.

  1. Test the nginx.conf configuration for syntax errors before reloading. Nginx validates the entire configuration and reports any problems.
sudo nginx -t
  1. Reload Nginx to apply the updated client_max_body_size value without dropping active connections.
sudo systemctl reload nginx

How to Verify the Upload Size Change

Nginx returns a 413 Request Entity Too Large error when a client sends a request body that exceeds the client_max_body_size limit. Upload a file larger than the old limit and smaller than the new limit to confirm the change took effect.

Test with curl by sending a file to the server:

curl -X POST -F "file=@largefile.zip" https://example.com/upload

A successful upload confirms that Nginx accepts the new file size limit.

Common Issues When Changing Upload Size in nginx.conf

PHP applications require matching limits.Nginx enforces client_max_body_size at the web server level. PHP enforces separate limits through upload_max_filesize and post_max_size in php.ini. Both limits must be equal to or greater than the desired upload size. A mismatch causes uploads to fail even when Nginx accepts the request.

Placing client_max_body_size in the wrong context.Nginx applies the most specific client_max_body_size value. A limit set in a location block overrides the server-level limit for that path. Verify that the directive appears in the correct context for the intended scope.

Setting client_max_body_size to 0 disables the check entirely.Nginx skips the body size check when the value is 0. This exposes the server to denial-of-service attacks from oversized payloads.