Client_max_body_size per $request_uri

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:
http://forum.nginx.org/read.php?2,221727,221727#msg-221727

On Sat, Jan 28, 2012 at 5:52 PM, skyroski [email protected] 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.

http://wiki.nginx.org/HttpMapModule


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

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:
http://www.nginx.org/en/docs/http/ngx_http_core_module.html#location

btw, http://wiki.nginx.org/IfIsEvil

[…]

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

Use try_files instead:
http://www.nginx.org/en/docs/http/ngx_http_core_module.html#try_files

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

On Sat, Jan 28, 2012 at 8:16 PM, skyroski [email protected] 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 :confused:

map $request_uri $uplimit {
~upload$ 200m;
default 1m;
}
client_max_body_size $uplimit;


O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

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:
http://forum.nginx.org/read.php?2,221727,221730#msg-221730