Same regex caching

Hi,

I wanted to ask if the same regex used in multiple conditions is the
same
cached-compiled version?
Eg I need to validate the all the (sub)request params before sending it
upstream:

location /foo {

if ($time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }
if ($arg_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }

}

#note that in the real app, I’ll have lots of repeated regex for email,
phone, used in every if directive

location /bar {

if ($arg_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }
set_form_input $form_time time_finished;
if ($form_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }

}

What I’m trying to do is send the params to redis with its lua scripting
engine handling the pre-validated request.
Redis is single-process while nginx can have multiple workers … so its
only fitting to let nginx do validation for redis :slight_smile:

Cheers

On Thu, Jul 21, 2011 at 10:18:06PM +0800, David Yu wrote:

if ($arg_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }
if ($form_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }

}

What I’m trying to do is send the params to redis with its lua scripting
engine handling the pre-validated request.
Redis is single-process while nginx can have multiple workers … so its
only fitting to let nginx do validation for redis :slight_smile:

No, each regex is compiled separately.


Igor S.

On Thu, Jul 21, 2011 at 10:43 PM, Igor S. [email protected] wrote:

if ($time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }
set_form_input $form_time time_finished;

Thanks for the quick reply.
I’m thinking maybe the nginx map can act as the regex cache for the same
ones.

set $map_regex_time $arg_time
if ( !$map_regex_time) return { 400; }

set_form_input $map_regex_time time_finished;
if (! $map_regex_time) return { 400; }

Since I’ll literally have hundreds of fields using the same regex, this
might help?

On Thu, Jul 21, 2011 at 10:53:51PM +0800, David Yu wrote:

location /foo {

if (! $map_regex_time) return { 400; }

Since I’ll literally have hundreds of fields using the same regex, this
might help?

I do not understand the issue.


Igor S.

On Thu, Jul 21, 2011 at 10:53 PM, David Yu [email protected]
wrote:

Eg I need to validate the all the (sub)request params before sending it
phone, used in every if directive
engine handling the pre-validated request.
if (!$map_regex_time) { return 400; }

set_form_input $map_regex_time time_finished;
if (!$map_regex_time) { return 400; }

Oops. Copy-paste err

On Fri, Jul 22, 2011 at 2:10 AM, Igor S. [email protected] wrote:

it
email,

What I’m trying to do is send the params to redis with its lua
ones.
I do not understand the issue.

map $map_arg_time $map_regex_time {
~/(\d\d):(\d\d):(\d\d)/ 1;
}

location /foo {
set $map_arg_time $arg_time
if ( !$map_regex_time ) return { 400; }

set_form_input $map_arg_time time_finished;
if ( !$map_regex_time ) return { 400; }

}

Since I’ll literally have hundreds of fields using the same regex for
validation, this might help?
If there are better ways to do validation, please do tell.

Cheers