Nginx - 将字符串转换为数字
Nginx - convert string to number
问题很简单,假设我在 nginx 中有一个变量:
set $test $upstream_status;
现在变量中的状态将为例如“200”。如何将此变量转换为数字以便像这样使用它:
return $test "test";
每个 nginx 变量都是一个字符串,唯一的例外是 $remote_binary_addr
一个(毕竟它仍然是一个 binary-packed 4 或 16 字节长度的字符串)和其他一些。然而,并非每个 nginx 指令都允许您使用变量来指定其参数,return
指令的 HTTP return 代码正是其中一种情况。
此外,无论你想做什么,你肯定是在尝试以错误的方式去做。来自特定上游 return 代码的 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
处理程序的所有指令,例如
location / {
proxy_pass ...
proxy_intercept_errors on;
error_page 301 @handler_301;
error_page 302 @handler_302;
...
}
location @handler_301 {
# you are free to use 'return 301 "anything"' here
...
}
location @handler_302 {
...
}
...
然而,您唯一可以拦截的 return 代码是 300 及以上。
问题很简单,假设我在 nginx 中有一个变量:
set $test $upstream_status;
现在变量中的状态将为例如“200”。如何将此变量转换为数字以便像这样使用它:
return $test "test";
每个 nginx 变量都是一个字符串,唯一的例外是 $remote_binary_addr
一个(毕竟它仍然是一个 binary-packed 4 或 16 字节长度的字符串)和其他一些。然而,并非每个 nginx 指令都允许您使用变量来指定其参数,return
指令的 HTTP return 代码正是其中一种情况。
此外,无论你想做什么,你肯定是在尝试以错误的方式去做。来自特定上游 return 代码的 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
处理程序的所有指令,例如
location / {
proxy_pass ...
proxy_intercept_errors on;
error_page 301 @handler_301;
error_page 302 @handler_302;
...
}
location @handler_301 {
# you are free to use 'return 301 "anything"' here
...
}
location @handler_302 {
...
}
...
然而,您唯一可以拦截的 return 代码是 300 及以上。