# Client\_max\_body\_size per $request\_uri

**URL:** <https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185>\
**Category:** NGINX\
**Created:** [January 28, 2012, 11:53am UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185 "2012-01-28T11:53:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 28, 2012, 11:53am UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185/1 "2012-01-28T11:53:05Z")

</div>

Hi, I have found similar topics on discussion with location based  
request but because I’m using a PHP software that by default handles  
everything through a single PHP file (including processing POST uploads  
of files) I would like to be able to use

if ($request\_uri ~\* upload$) {  
client\_max\_body\_size 200M;  
}

for example, so that all URI with the word upload at the end will  
automatically get set a larger client\_max\_body\_size.

But given that the above syntax is currently impossible. How would I be  
able to achieve the same result?

The way my nginx conf works is via:

upstream php {  
server unix:/php.sock;  
}

if (!-e $request\_filename) {  
rewrite ^/(.\*)$ /index.php/$1 last;  
break;  
}

location ~ .php$ {  
fastcgi\_pass php;  
include fastcgi\_params;  
}

Posted at Nginx Forum:

> **[client\_max\_body\_size per $request\_uri](https://forum.nginx.org/read.php?2,221727,221727#msg-221727)**
>
> Hi, I have found similar topics on discussion with location based request but because I'm using a PHP software that by default handles everything through a single PHP file (including processing POST uploads of files) I would like to be able to use if...

---

<div class="post-metadata">

**Author:** ![skyroski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skyroski](https://www.ruby-forum.com/u/skyroski)\
**Post date:** [January 28, 2012, 12:37pm UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185/2 "2012-01-28T12:37:01Z")

</div>

On Sat, Jan 28, 2012 at 5:52 PM, skyroski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I have found similar topics on discussion with location based  
> request but because I’m using a PHP software that by default handles  
> everything through a single PHP file (including processing POST uploads  
> of files) I would like to be able to use
> 
> if ($request\_uri ~\* upload$) {  
> client\_max\_body\_size 200M;  
> }

My guess says map would work.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![skyroski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skyroski](https://www.ruby-forum.com/u/skyroski)\
**Post date:** [January 28, 2012, 12:57pm UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185/3 "2012-01-28T12:57:59Z")

</div>

On Saturday 28 January 2012 14:52:38 skyroski wrote:

> Hi, I have found similar topics on discussion with location based  
> request but because I’m using a PHP software that by default handles  
> everything through a single PHP file (including processing POST uploads  
> of files) I would like to be able to use
> 
> if ($request\_uri ~\* upload$) {  
> client\_max\_body\_size 200M;  
> }

If you want something like “if ($uri …” then you should use the  
“location”  
directive:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html)**

btw, [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil)

[…]

> if (!-e $request\_filename) {  
> rewrite ^/(.\*)$ /index.php/$1 last;  
> break;  
> }

Use try\_files instead:

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html)**

e.g.:

```
location / {
    try_files $uri @index_php;
}

location ~* upload$ {
    client_max_body_size 200M;
    ...
}

location ~ \.php$ {
    fastcgi_pass php;
    include fastcgi_params;
}

location @index_php {
    fastcgi_pass php;
    include fastcgi_params;
    fastcgi_param PATH_INFO $uri;
    fastcgi_param SCRIPT_NAME /path/to/your/index.php;
}

```

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![skyroski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skyroski](https://www.ruby-forum.com/u/skyroski)\
**Post date:** [January 28, 2012, 2:41pm UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185/4 "2012-01-28T14:41:17Z")

</div>

On Sat, Jan 28, 2012 at 8:16 PM, skyroski [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> internal;  
> All the rest I didn’t have to change with the exception of adding an  
> extra clause to the location .php$ block to restore PHP\_VALUE to  
> default settings.
> 
> I understand around-abouts why IF is evil but if it’s supposed to be  
> safe for rewrites and return then I’d rather take advantage of that  
> instead of using location / with try files so that I can deal with query  
> strings attachments easier and it ends up shorter to implement.

too bad this doesn’t work 😕

map $request\_uri $uplimit {  
~upload$ 200m;  
default 1m;  
}  
client\_max\_body\_size $uplimit;

–  
O\< ascii ribbon campaign - stop html mail - [www.asciiribbon.org](http://www.asciiribbon.org)

---

<div class="post-metadata">

**Author:** ![skyroski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9a28c/32.png) [@skyroski](https://www.ruby-forum.com/u/skyroski)\
**Post date:** [January 28, 2012, 2:17pm UTC](https://www.ruby-forum.com/t/client-max-body-size-per-request-uri/216185/5 "2012-01-28T14:17:46Z")

</div>

For the record, I used a combination of a similar issue in the mailing  
list and a combination of what @Valentin suggested.

In the end, this is what works for me:

if ($request\_uri ~\* upload$) {  
rewrite ^/(.\*)$ /upload/$1 last;  
}

location ~ /upload/(.\*)$ {  
internal;  
client\_max\_body\_size 200M;  
fastcgi\_param PHP\_VALUE …;  
fastcgi\_pass php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root/index.php;  
include fastcgi\_params;  
fastcgi\_param QUERY\_STRING /$1;  
fastcgi\_param DOCUMENT\_URI /index.php;  
fastcgi\_param SCRIPT\_FILENAME /index.php;  
}

All the rest I didn’t have to change with the exception of adding an  
extra clause to the location .php$ block to restore PHP\_VALUE to  
default settings.

I understand around-abouts why IF is evil but if it’s supposed to be  
safe for rewrites and return then I’d rather take advantage of that  
instead of using location / with try files so that I can deal with query  
strings attachments easier and it ends up shorter to implement.

@Valentin’s solution would have meant I had to maintain 3 different  
fastcgi\_pass php location blocks as one would also be required for  
location ~ upload$ {.

Thanks for all the hints and tips.

Posted at Nginx Forum:

> **[Re: client\_max\_body\_size per $request\_uri](https://forum.nginx.org/read.php?2,221727,221730#msg-221730)**
>
> Hi, I have found similar topics on discussion with location based request but because I'm using a PHP software that by default handles everything through a single PHP file (including processing POST uploads of files) I would like to be able to use if...
