In nginx, variables come in multiple forms. Generally within the same location, variables are consistent. However, when transferring from one location to another, variables may change - some will be modified, others will end their lifetime.

Considering the combinations of location jump types and variable types, variable lifetimes become quite complex and interesting...

Methods to transfer from one location to another:

  • rewrite (rewrite_by_lua, rewrite_by_luafile)
  • proxy_pass

  • The following are modules provided by ngx_openresty:
  • ngx.exec
  • ngx.location.capture

Variable Types

  • nginx core variables
  • Custom variables defined in nginx config: set $foo 'bar'
  • Header-related variables

Behavior Rules

location changenginx corecustomheaders
rewriteURI-related variables changeunchanged or overwrittenunchanged
proxy_passURI-related change, args unchanged, $remote_addr becomes 127.0.0.1emptyproxy_pass, custom headers unchanged
ngx.execURI-related variables, args lostunchanged or overwrittenunchanged
ngx.location.captureURI-related variables, args lostemptyunchanged

References:

  • http://wiki.nginx.org/HttpLuaModule
  • http://nginx.org/en/docs/http/ngx_http_core_module.html