Skip to content

Commit eeee406

Browse files
authored
doc: ngx.var.VARIABLE is available in the balancer_by_lua* context (openresty#1761)
1 parent 49937fb commit eeee406

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3464,7 +3464,7 @@ ngx.var.VARIABLE
34643464

34653465
**syntax:** *ngx.var.VAR_NAME*
34663466

3467-
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
3467+
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, balancer_by_lua**
34683468

34693469
Read and write Nginx variable values.
34703470

doc/HttpLuaModule.wiki

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,7 @@ The data chunk and "eof" flag passed to the downstream Nginx output filters can
28082808
28092809
'''syntax:''' ''ngx.var.VAR_NAME''
28102810
2811-
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*''
2811+
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, balancer_by_lua*''
28122812
28132813
Read and write Nginx variable values.
28142814

t/045-ngx-var.t

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Test::Nginx::Socket::Lua;
77

88
repeat_each(2);
99

10-
plan tests => repeat_each() * (blocks() * 2 + 7);
10+
plan tests => repeat_each() * (blocks() * 2 + 9);
1111

1212
#no_diff();
1313
#no_long_string();
@@ -228,3 +228,53 @@ GET /test?hello
228228
--- error_log
229229
variable "query_string" not changeable
230230
--- error_code: 500
231+
232+
233+
234+
=== TEST 12: get a variable in balancer_by_lua_block
235+
--- http_config
236+
upstream balancer {
237+
server 127.0.0.1;
238+
balancer_by_lua_block {
239+
local balancer = require "ngx.balancer"
240+
local host = "127.0.0.1"
241+
local port = ngx.var.port;
242+
local ok, err = balancer.set_current_peer(host, port)
243+
if not ok then
244+
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
245+
return ngx.exit(500)
246+
end
247+
}
248+
}
249+
server {
250+
# this is the real entry point
251+
listen 8091;
252+
location / {
253+
content_by_lua_block{
254+
ngx.print("this is backend peer 8091")
255+
}
256+
}
257+
}
258+
server {
259+
# this is the real entry point
260+
listen 8092;
261+
location / {
262+
content_by_lua_block{
263+
ngx.print("this is backend peer 8092")
264+
}
265+
}
266+
}
267+
--- config
268+
location =/balancer {
269+
set $port '';
270+
set_by_lua_block $port {
271+
local args, _ = ngx.req.get_uri_args()
272+
local port = args['port']
273+
return port
274+
}
275+
proxy_pass http://balancer;
276+
}
277+
--- pipelined_requests eval
278+
["GET /balancer?port=8091", "GET /balancer?port=8092"]
279+
--- response_body eval
280+
["this is backend peer 8091", "this is backend peer 8092"]

0 commit comments

Comments
 (0)