Forum: NGINX error_page can not work in if directive

Posted by xinghua_hi (Guest)
on 2012-12-25 14:07
(Received via mailing list)
I want to display different pages when 413 code return, for example

set $var 1;
if ($var = 1) {
     error_page 413 /413_old.html;
}
if ($var =2) {
    error_page 413 /413_new.html;
}

 error_page can not work, but I write error_page directive in location 
block
directly instead of using if directive, it can work normally

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,234417,234417#msg-234417
Posted by xinghua_hi (Guest)
on 2012-12-25 15:37
(Received via mailing list)
please help me, thank you

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,234417,234420#msg-234420
Posted by Maxim Dounin (Guest)
on 2012-12-25 15:42
(Received via mailing list)
Hello!

On Tue, Dec 25, 2012 at 08:06:45AM -0500, xinghua_hi wrote:

>  error_page can not work, but I write error_page directive in location block
> directly instead of using if directive, it can work normally

There several ways to do what you want.  E.g. you may use
variables in the error page specified, e.g.

   error_page 413 /413.$var.html;

or use an error_page with additional processing for the page in
question, e.g.

   error_page 413 /413.html;

   location = /413.html {
       if ($new) {
           rewrite ^ /413.new.html;
       }
   }


--
Maxim Dounin
http://nginx.com/support.html
Posted by xinghua_hi (Guest)
on 2012-12-25 16:10
(Received via mailing list)
thank you very much, but i sitll  want to know why error_page can't work
normally in if block.
I see the error_page 's context can be " if in location" in the 
document.

the whole location conf  like below, error_page can not work:

 client_max_body_size 50k;
  location / {
            set $var "haha";
            if ($var = "haha") {
                error_page 413 /413.html;
            }
        }

but

client_max_body_size 50k;
  location / {
             error_page 413 /413.html;
        }
it works!

and

  location / {
            set $var "haha";
            if ($var = "haha") {
                error_page 404 /404.html;
            }
        }

it  also works!

thanks!

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,234417,234426#msg-234426
Posted by Maxim Dounin (Guest)
on 2012-12-25 16:27
(Received via mailing list)
Hello!

On Tue, Dec 25, 2012 at 10:09:45AM -0500, xinghua_hi wrote:

>                 error_page 413 /413.html;
>             }
>         }
>
> but
>
> client_max_body_size 50k;
>   location / {
>              error_page 413 /413.html;
>         }
> it works!

The 413 error is generated once location configuration is
determinded, and this happens before location-level rewrite module
directives (the "if" directive in particular) are executed.  Hence
error_page 413 configure inside the "if" doesn't make any
difference.

(Please also note that in general it's a good idea to avoid using
the "if" directive, see http://wiki.nginx.org/IfIsEvil.)

--
Maxim Dounin
http://nginx.com/support.html
Posted by xinghua_hi (Guest)
on 2012-12-25 17:48
(Received via mailing list)
thanks, but a new question comming。
I try to resolve my problem according to your method:
(1)
“set" is also an location-level rewrite module directive, so the conf 
like
this
 location / {
           root XXX;
            set $var "haha";
            error_page 413 /413.${var}.html;
        }

will return 404, but 413.haha.html  exists my root dir。I see the debug 
log ,
find some log like
        http finalize request: 404, "/413..html?"
obviously, nginx  takes $var as an empty string, I  think the reason is 
also
that 413 error is generated  before set directive? so , how to use 
variable
in error_page uri?

(2)
if  I  writer conf  like this:
location / {
      set $var "haha";
       error_page 413  /413.html;
}

location = /413.html {
      if ($var)  {
           rewriter ^  /413.new.html;
     }
}

the key problem is   how can i  pass the $var to location /413.html?  I 
try
it , the second $var is empty(I also wonder about the result because I
remember when internal redirect,  the location can share the variable) 。

Thank you .

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,234417,234437#msg-234437
Posted by Maxim Dounin (Guest)
on 2012-12-25 18:24
(Received via mailing list)
Hello!

On Tue, Dec 25, 2012 at 11:47:58AM -0500, xinghua_hi wrote:

>
> will return 404, but 413.haha.html  exists my root dir。I see the debug log ,
> find some log like
>         http finalize request: 404, "/413..html?"
> obviously, nginx  takes $var as an empty string, I  think the reason is also
> that 413 error is generated  before set directive? so , how to use variable
> in error_page uri?

Same problem here: the "set" directive isn't executed, and hence
the $var isn't "haha" but uninitialized and evaluates to an empty
string.

You have to make $var variable available by other means (e.g. set
the variable at server level, or make it available via map{}
directive).

>      }
> }
>
> the key problem is   how can i  pass the $var to location /413.html?  I try
> it , the second $var is empty(I also wonder about the result because I
> remember when internal redirect,  the location can share the variable) 。

And the same problem here: "set $var "haha";" in the location /
isn't executed, and hence $var remains uninitialized.

--
Maxim Dounin
http://nginx.com/support.html
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.