0

I am looking to limit the size of the client body based on the content type in nginx.

location / {
    if ($content_type = "application/json") {
            client_max_body_size 1M;
            client_body_buffer_size 512K;
    }
    ...
    ...
}

I've tried the above if condition which fails with below error.

nginx: [emerg] "client_max_body_size" directive is not allowed here

I have also tried using this without any effect

location ~* ^.+\.json {
   client_max_body_size 1M;
   client_body_buffer_size 512K;
}
0

You use a map directive in your http block but outside any server blocks.

map $http_content_type $upload_limit {
    default 0;
    application/json 1M;
}

This sets the second variable based upon the value of the first, so by default it'll be 0 but if content type header is application/json it'll be 1M.

Now you use your custom variable like this client_max_body_size $upload_limit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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