Changing upstream url

Hi guys,

Is there a way to modify the upstream url?

I’m trying to use ngx.req.set_uri() but that does not seem to affect the upstream_url.

Edit:
Clarification: we are defining our api with “strip_url”: false, and we need to modify the match_string part of the constructed url (“upstream_url + match_string”).

Thanks,
Alex

Hi,

As of 0.11.0, and as documented in the Changelog and Upgrade path of that version, the way to update the upstream URL is via the $upstream_uri NGINX variable, accessible in the Lua-land as ngx.var.upstream_uri.

The upstream URI is now determined via the Nginx $upstream_uri variable. Custom plugins using the ngx.req.set_uri() API will not be taken into consideration anymore. One must now set the ngx.var.upstream_uri variable from the Lua land. #2519

Additionally, you can access the ngx.ctx.router_matches variable defined here, which contains values the Router detected for a given API (like the Host it matched with, or the uri or even PCRE capturing groups if the uri was a regex, etc… See this declaration:

  matches = {
    uri_captures = matches.uri_captures,
    uri = matches.uri,
    host = matches.host,
    method = matches.method,
  }
}

If this isn’t documented, it is because it should not be considered stable or part of the public Lua API in any way yet, and is subject to changes. We will later provide a stable, documented public Lua API for plugins developers, stay tuned.

2 Likes