Openresty: Difference between ngx.var.arg and ngx.req.get_uri_args


Both ngx.var.arg_x and ngx.req.get_uri_args["x"] are to get the parameters in the request uri, for example:

https://tutorialspots.com/?test=1

In order to get the input parameter test, either of the following methods will work:

local test = ngx.var.arg_test
local test= ngx.req.get_uri_args["test"]

The difference is that when there are multiple parameters with the same name in the request uri, the method of ngx.var.arg_x is to take the first value, and the method of ngx.req_get_uri_args[“x”] is to return a table, which stores All values ​​for this parameter.

For example, the uri request is as follows:

http://127.0.0.1?code=1&code=2&code=3

local code = ngx.var.code
local code = ngx.req.get_uri_args["code"]

When there are multiple parameters with the same name in the request uri, ngx.var.arg is to take the first value "1"
ngx.req.get_uri_args["x"] is to return a table, which stores the parameters of the parameter all values. The returned table list is ["1", "2", "3"]
ngx.req.get_uri_args["x"] is an upgraded version of ngx.var.arg_x

Leave a Reply