[英]Use variable in lua and nginx
I have a variable $aet
that I initialize in lua, but I wish I could use it in nginx too.我有一个在 lua 中初始化的变量$aet
,但我希望我也可以在 nginx 中使用它。
Here is my code:这是我的代码:
location /getIp {
default_type 'application/json';
rds_json on;
content_by_lua '
if ngx.var.host:match("(.*).nexus$") ~= nil then
aet = ngx.var.host:match("(.-)%.")
$aet = aet;
end
';
postgres_pass database;
postgres_query "SELECT ip FROM establishment_view WHERE aet = $aet";
postgres_output rds;
}
It does not work because in the query it does not know the variable aet :它不起作用,因为在查询中它不知道变量 aet :
nginx: [emerg] unknown "aet" variable nginx:[emerg] 未知的“aet”变量
RTFM https://github.com/openresty/lua-nginx-module#ngxvarvariable RTFM https://github.com/openresty/lua-nginx-module#ngxvarvariable
Read and write Nginx variable values.读取和写入 Nginx 变量值。
value = ngx.var.some_nginx_variable_name
ngx.var.some_nginx_variable_name = value
Note that only already defined nginx variables can be written to.请注意,只能写入已定义的 nginx 变量。 For example:例如:
location /foo {
set $my_var ''; # this line is required to create $my_var at config time
content_by_lua_block {
ngx.var.my_var = 123;
...
}
}
That is, nginx variables cannot be created on-the-fly.也就是说,不能即时创建 nginx 变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.