1

This question is similar to Apply client_max_body_size for a single URL only but different due to the rewrite.

I'd like to allow larger body size on endpoint lg, but not on the others. My existing configuration looks like this:

location /xyz/ {
  rewrite ^/xyz/(.*) /$1 break;
  proxy_pass http://{{proxy_host}}:{{proxy_port}}/;
}

I want to do something like the below, but keep either getting errors, or it simply doesn't work:

location /xyz/ {
  rewrite ^/xyz/(.*) /$1 break;
  ...
  location /xyz/lg {
    client_max_body_size 2m;
  }
}

I tried using regex for the nested url but only got:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location,

  • Please edit your question and show the actual location block contents. The ... is probably significant. – Richard Smith Aug 27 '20 at 7:56
  • Thanks @RichardSmith done – Des Horsley Aug 27 '20 at 22:48
  • Your post does not contain the configuration which resulted in the error message you posted. Please edit your post to show the actual configuration which resulted in the error message. – Michael Hampton Aug 28 '20 at 18:19
1

The rewrite...break is unnecessary, as proxy_pass with a trailing / performs the same task, so your original location block can be simplified to:

location /xyz/ {
    proxy_pass http://{{proxy_host}}:{{proxy_port}}/;
}

The client_max_body_size statement needs to be placed within a location block, and that block also needs a proxy_pass statement, as that directive is not inherited.

For example:

location /xyz/ {
    proxy_pass http://{{proxy_host}}:{{proxy_port}}/;
}
location /xyz/lg {
    client_max_body_size 2m;
    proxy_pass http://{{proxy_host}}:{{proxy_port}}/lg;
}

You can probably nest the location blocks, but there is no point as there is nothing to inherit.

Notice that the lg part of the URI is added to both the new location statement and the new proxy_pass statement.

The example will set the client_max_body_size for any URI that begins with /xyz/lg, e.g. /xyz/lg, /xyz/lgfoo and /xyz/lg/bar.

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.