The question is simple let's say I have a variable in nginx:
set $test $upstream_status;
And now the status in the variable will be for example "200". How I can convert this variable to number in order to use it like so:
return $test "test";
CodePudding user response:
Every nginx variable is a string, with the only exception of $remote_binary_addr
one (which is still a binary-packed string of 4 or 16 bytes length after all) and a few others. However not every nginx directive allows you to use variable(s) to specify its parameters, and the HTTP return code for the return
directive is exactly one of those cases.
Moreover, whatever you are trying to do, you definitely trying to do it in a wrong way. All the directives from the ngx_http_rewrite_module module (including the return
one) are being executed at the very early stage of request processing, before anything is being send to the upstream, not even speaking about something that should be received. Usually such a thing can be achieved using the proxy_intercept_errors on;
setting and specifying an error_page
handler for the specific upstream return code, e.g.
location / {
proxy_pass ...
proxy_intercept_errors on;
error_page 301 @handler_301;
error_page 302 @handler_302;
...
}
location @handler301 {
# you are free to use 'return 301 "anything"' here
...
}
location @handler302 {
...
}
...
However the only return codes you can intercept with this are 300 and above.