I'm trying to set client_max_body_size conditionally. I've followed the configuration suggested in the answer to Nginx set a variable conditionally, but it is not working for me.

The following configuration file is sufficient to reproduce:

daemon off;
worker_processes 1;
error_log /dev/stderr debug;
events {
  worker_connections 1024;
http {
  access_log /dev/stdout;
  server {
    listen 80;
    location /test {
      set $cmbs 1m;
      if ($arg_long) {
        set $cmbs 5m;
      client_max_body_size $cmbs;
      echo $cmbs;

When I try to start Nginx with this configuration, it halts and complains

nginx: [emerg] "client_max_body_size" directive invalid value in /opt/nginx/nginx.conf:16

If I remove the offending directive, Nginx starts and I echo the set value back in the way I expect.

I am running openresty/ I've also tried the above small config on nginx/1.9.4.

  • What do you mean with $arg_long? – rubo77 Jun 3 '20 at 9:28
  • 5 years later I don't entirely recall. From context, I think it's a stand-in for "some kind of test that says whether or not I want to accept a long argument". For example, users who present the correct auth-token might be allowed large uploads while anonymous users get a small limit. Or requests from a set of white-listed IPs might get the larger limit. – Nathaniel Waisbrot Jun 5 '20 at 18:26

Nginx as of version 1.9.4 does not support variable client_max_body_size.

  • I get the exact same error message. Did this work for you? – Nathaniel Waisbrot Sep 15 '15 at 18:59
  • Nope, updated answer. – h0tw1r3 Sep 16 '15 at 1:13

Maybe you can solve this by defining an upstream and use proxy_pass to redirect at the right condition:


  • I believe the problem here happens before a call to proxy_pass. If the request is larger than Nginx's limit, then it's going to fail the request before it considers passing to an upstream. – Nathaniel Waisbrot Jun 5 '20 at 18:29

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.