Problem with error_handler (redirect loop?)

Maxim Dounin mdounin at
Tue Jul 6 00:19:58 MSD 2010


On Mon, Jul 05, 2010 at 03:48:21PM -0400, bkirkbri wrote:

> I'm running into a problem where requests that trigger an error_page
> handler get caught in a redirect loop and eventually fail with a 500
> response.  It looks like this _may_ be related to our server_name being
> a regex with captures, as that is what nginx is doing right before the
> error output.


>         server_name             ~^(.)(.)(.).*$;
>                 set                             $host_1 $1;
>                 set                             $host_2 $2;
>                 set                             $host_3 $3;

You are using nonexitant enumerated captures here as they are 
gone as soon as you execute another regexp while server rewrite 
directives (including "set") are re-executed after internal 

General rule is: never use enumerated captures from server_name 
regexp as long as you have other regexps in server block in 
question.  Or, better: never use enumerated captures from 

This particular problem with malloc(-2) should be fixed 0.8.*.  
Also in 0.8.* you may use named captures in server_name instead of 
enumerated, they will survive internal redirects.

For 0.7.* just avoid using captures in server_name.  If you really 
have to set something based on Host header - use 

    if ($http_host ~ "^(.)(.)(.).*$") {
        set $host_1 $1;
        set $host_2 $2;
        set $host_3 $3;

at server level instead.


>                recursive_error_pages   on;
>                error_page                              500 502 503 /server_error.html;

And you switched off protection from recursive error pages, and 
you have error_page defined for 500 Internal Server Error.  There 
is no surprise you see loop.

Maxim Dounin

More information about the nginx mailing list