Why openresty can not modify ngx.var.host?
Mickle
Thank you for your answer, But I found that neither ngx.req.set_uri
nor ngx.req.set_uri_args
can modify the ngx.var.request_uri
.
The ngx.req.set_uri(uri, true)
can jump to a new location, but the ngx.var.request_uri
is old in the new location. It can only mofidy the uri which to upstream. Is there some way to modify the value of ngx.var.request_uri
?
ccxhwmy
ngx.req.set_uri_args(args)
ngx.req.set_uri(uri,jump) --uri only ,Do not contain parameter
The optional boolean jump argument can trigger location rematch (or location jump) as ngx_http_rewrite_module's rewrite directive, that is, when jump is true (default to false), this function will never return and it will tell Nginx to try re-searching locations with the new URI value at the later post-rewrite phase and jumping to the new location.
if you want to give off all parameter,use follow method
ngx.var.args=nil
I config the nginx.conf
like:
location /request_aaa {
rewrite_by_lua_block {
ngx.log(ngx.ERR, "request_uri in location request_aaa: ", ngx.var.request_uri)
ngx.req.set_uri("/request_bbb", true)
}
}
location /request_bbb {
rewrite_by_lua_block {
ngx.log(ngx.ERR, "request_uri in location request_bbb: ", ngx.var.request_uri)
ngx.log(ngx.ERR, "uri in location request_bbb: ", ngx.var.uri)
}
proxy_pass http://127.0.0.1:8888;
}
I make a request like:
curl "http://127.0.0.1/request_aaa"
The error.log is:
2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:64):2: request_uri in location request_aaa: /request_aaa, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"
2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:71):2: request_uri in location request_bbb: /request_aaa, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"
2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:71):3: uri in location request_bbb: /request_bbb, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"
We can find that ngx.var.request_uri
does not change in the location /request_bbb
show response from server 127.0.0.1:8888