All Products
Search
Document Center

Server Load Balancer:Request logic functions

Last Updated:Sep 21, 2026

Request logic functions let you inspect incoming requests — querying the server address, URI components, headers, cookies, and more — and use those values to build routing logic in your ALB forwarding rule scripts.

All functions follow the same two-mode pattern:

  • Without `pattern`: returns the queried value as a string, numeric, or dictionary.

  • With `pattern`: compares the value against match conditions and returns true or false.

Two match types are supported for pattern:

Match typeSyntaxDescription
Brute-force'value'Compares the values based on a brute-force algorithm (default)
Regular expression're:pattern'Prefix the pattern with re: to use a leading regex

server_addr

server_addr() → String

Returns the IP address of the server that received the current request.

s_addr = server_addr()
say(concat('s_addr:', s_addr))

server_port

server_port() → Numeric

Returns the port on which the server received the current request.

s_port = server_port()
say(concat('s_addr:', s_port))

client_addr

client_addr() → String

Returns the client IP address.

Important

If NAT is configured, the value returned by client_addr may not be the real client IP address.

c_addr = client_addr()
say(concat('c_addr:', c_addr))

client_port

client_port() → Numeric

Returns the client port.

c_port = client_port()
say(concat('c_port:', c_port))

req_uri

req_uri([pattern String]) → String | Boolean

Without pattern, returns the request URI, excluding query parameters. With pattern, compares the URI against match conditions.

Example

# req_uri
say(concat('req_uri: ', req_uri()))
if req_uri('/path1/path2') {
    say('req_uri: plain match')
}
if req_uri('re:/path[0-9]/path[0-9]') {
    say('req_uri: regex match')
}

For a request to /path1/path2?mode=ip:

req_uri: /path1/path2
req_uri: plain match
req_uri: regex match

req_uri_basename

req_uri_basename([pattern String]) → String | Boolean

Without pattern, returns the filename from the request URI, excluding the extension. With pattern, compares the filename against match conditions.

Filename examples:

URIFilename
/document_detail/30360.html30360
/M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp42-1_g
/tarball/foo.tar.bz2foo

Example

# req_uri_basename
basename = req_uri_basename()
say(concat('req_uri_basename: ', basename, ' ', len(basename)))
if req_uri_basename('foo') {
    say('req_uri_basename: plain match')
}
if req_uri_basename('re:^f.*') {
    say('req_uri_basename: regex match')
}

For a request to /path1/path2/foo.tar.bz2:

req_uri_basename: foo 3
req_uri_basename: plain match
req_uri_basename: regex match

req_uri_ext

req_uri_ext([pattern String]) → String | Boolean

Without pattern, returns the file extension from the request URI. With pattern, compares the extension against match conditions.

Extension examples:

URIExtension
/document_detail/30360.html.html
/M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4.mp4
/tarball/foo.tar.bz2.tar.bz2

Example

# req_uri_ext
ext = req_uri_ext()
say(concat('req_uri_ext: ', ext, ' ', len(ext)))
if req_uri_ext('.tar.bz2') {
    say('req_uri_ext: plain match')
}
if req_uri_ext('re:\.tar\.bz[0-2]') {
    say('req_uri_ext: regex match')
}

For a request to /path1/path2/foo.tar.bz2:

req_uri_ext: .tar.bz2 8
req_uri_ext: plain match
req_uri_ext: regex match

req_uri_seg

req_uri_seg([idx Numeric]) → Dictionary

Returns URI segments split by forward slashes (/). Without idx, returns all segments. With idx, returns segments starting from that index (inclusive).

Indexes start at 1 from the leftmost segment. Each segment can contain up to 128 characters; characters beyond this limit are dropped. When you retrieve a segment from the returned dictionary by index, the function checks whether that segment is empty.

Example

# req_uri_seg
def echo_each(k, v, u) {
    say(concat(get(u, 'msg'), ' : segs[', k, ']=', v))
}
# fetch all segments
segs = req_uri_seg()
foreach(segs, echo_each, ['msg'='req_uri_seg()'])
# fetch segments from idx 3
segs = req_uri_seg(3)
if get(segs, 3) {
    say(concat('req_uri_seg(3): segs[3]=', get(segs, 3)))
}
if get(segs, 4) {
    say(concat('req_uri_seg(3): segs[4]=', get(segs, 4)))
}
if get(segs, 5) {
    say(concat('req_uri_seg(3): segs[5]=', get(segs, 5)))
}

For a request to /path1/path2/path3/path4?mode=req2:

req_uri_seg() : segs[1]=path1
req_uri_seg() : segs[2]=path2
req_uri_seg() : segs[3]=path3
req_uri_seg() : segs[4]=path4
req_uri_seg(3): segs[3]=path3
req_uri_seg(4): segs[4]=path4

req_uri_arg

req_uri_arg(name String, [pattern String]) → String | Boolean

Returns the value of the specified query parameter. With pattern, compares the value against match conditions.

ParameterDescription
nameName of the query parameter
patternMatch conditions (brute-force or re: regex)

Return values without `pattern`:

  • Parameter exists: returns its value as a string.

  • Parameter does not exist: returns false.

Return values with `pattern`:

  • Parameter exists and matches: returns true.

  • Parameter exists but does not match, or does not exist: returns false.

Example

# req_uri_arg
uid = req_uri_arg('uid')
if uid {
    say(concat('found uid ', uid))
} else {
    say('not found uid')
}
uid_chk = req_uri_arg('uid', '058334')
if uid_chk {
    say('check uid ok. plain mode')
} else {
    say('check uid fail. plain mode')
}
uid_chk = req_uri_arg('uid', 're:[0-9]+')
if uid_chk {
    say('check uid ok. regex mode')
} else {
    say('check uid fail. regex mode')
}
RequestResponse
?mode=req4&uidnot found uid / check uid fail. plain mode / check uid fail. regex mode
?mode=req4&uid=found uid / check uid fail. plain mode / check uid fail. regex mode
?mode=req4&uid=12345found uid 12345 / check uid fail. plain mode / check uid ok. regex mode

req_uri_query_string

req_uri_query_string([pattern String]) → String | Boolean

Without pattern, returns the full query string, excluding the leading ?. With pattern, compares the query string against match conditions.

Example

# req_uri_query_string
say(concat('req_uri_query_string: ', req_uri_query_string()))
if req_uri_query_string('mode=') {
    say('check uri query string ok. plain mode')
} else {
    say('check uri query string fail. plain mode')
}
if req_uri_query_string('re:mode=[0-9a-z]+') {
    say('check uri query string ok. regex mode')
} else {
    say('check uri query string fail. regex mode')
}

For a request to /path1/path2/path3/path4?mode=req5&token=34Deasd#243:

req_uri_query_string: mode=req5&token=34Deasd
check uri query string fail. plain mode
check uri query string ok. regex mode

req_scheme

req_scheme([pattern String]) → String | Boolean

Without pattern, returns the request scheme (for example, http or https). With pattern, compares the scheme against match conditions.

Example

# req_scheme
say(concat('req_scheme: ', req_scheme()))
if req_scheme('https') {
    say('check scheme ok. plain mode')
} else {
    say('check scheme fail. plain mode')
}
if req_scheme('re:https?') {
    say('check scheme ok. regex mode')
} else {
    say('check scheme fail. regex mode')
}

For a request to http://xx..:

req_scheme: http
check scheme fail. plain mode
check scheme ok. regex mode

req_method

req_method([pattern String]) → String | Boolean

Without pattern, returns the HTTP method of the request. With pattern, compares the method against match conditions.

Example

# req_method
say(concat('req_method: ', req_method()))
if req_method('GET') {
    say('check method ok. plain mode')
} else {
    say('check method fail. plain mode')
}
if req_method('re:(GET|POST)') {
    say('check method ok. regex mode')
} else {
    say('check method fail. regex mode')
}

For a POST /xxxx/xxx request:

req_method: POST
check method fail. plain mode
check method ok. regex mode

req_host

req_host([pattern String]) → String | Boolean

Without pattern, returns the value of the Host request header. With pattern, compares the value against match conditions.

Example

# req_host
say(concat('req_host: ', req_host()))
if req_host('image.developer.aliyundoc.com') {
    say('check host ok. plain mode')
} else {
    say('check host fail. plain mode')
}
if req_host('re:.+\.y\.z\.com') {
    say('check host ok. regex mode')
} else {
    say('check host fail. regex mode')
}

For a request with Host: image.developer.aliyundoc.com:

req_host: image.developer.aliyundoc.com
check host ok. plain mode
check host fail. regex mode

req_user_agent

req_user_agent([pattern String]) → String | Boolean

Without pattern, returns the value of the User-Agent request header. With pattern, compares the value against match conditions.

Example

# req_user_agent
say(concat('req_user_agent: ', req_user_agent()))
if req_user_agent('Mozilla') {
    say('check user_agent ok. plain mode')
} else {
    say('check user_agent fail. plain mode')
}
if req_user_agent('re:^Mozilla') {
    say('check user_agent ok. regex mode')
} else {
    say('check user_agent fail. regex mode')
}

For a request with User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64):

req_user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
check user_agent fail. plain mode
check user_agent ok. regex mode

req_referer

req_referer([pattern String]) → String | Boolean

Without pattern, returns the value of the Referer request header. With pattern, compares the value against match conditions.

Example

# req_referer
say(concat('req_referer: ', req_referer()))
if req_referer('https://example.aliyundoc.com/******00003') {
    say('check referer ok. plain mode')
} else {
    say('check referer fail. plain mode')
}
if req_referer('re:https://foo\.bar\.cn/\*+[0-9]+') {
    say('check referer ok. regex mode')
} else {
    say('check referer fail. regex mode')
}

For a request with Referer: https://example.aliyundoc.com/******00003:

req_referer: https://example.aliyundoc.com/******00003
check referer ok. plain mode
check referer fail. regex mode

req_cookie

req_cookie(name String, [pattern String]) → String | Boolean

Returns the value of the specified cookie. With pattern, compares the cookie value against match conditions.

ParameterDescription
nameName of the cookie
patternMatch conditions (brute-force or re: regex)

Return values without `pattern`:

  • Cookie exists: returns its value as a string.

  • Cookie does not exist: returns false.

Return values with `pattern`:

  • Cookie exists and matches: returns true.

  • Cookie exists but does not match, or does not exist: returns false.

Example

# req_cookie
uid = req_cookie('uid')
if uid {
    say(concat('found cookie uid ', uid))
} else {
    say('not found cookie uid')
}
uid_chk = req_cookie('uid', '058334')
if uid_chk {
    say('check cookie uid ok. plain mode')
} else {
    say('check cookie uid fail. plain mode')
}
uid_chk = req_cookie('uid', 're:^[0-9]+')
if uid_chk {
    say('check cookie uid ok. regex mode')
} else {
    say('check cookie uid fail. regex mode')
}

For a request with Cookie: uid=123456; token=value2:

found cookie uid 123456
check cookie uid fail. plain mode
check cookie uid ok. regex mode

req_first_x_forwarded

req_first_x_forwarded([pattern String]) → String | Boolean

Without pattern, returns the first IP address in the X-Forwarded-For request header. With pattern, compares that address against match conditions.

Example

# req_first_x_forwarded
say(concat('req_first_x_forwarded: ', req_first_x_forwarded()))
if req_first_x_forwarded('1.1.1.1') {
    say('check first_x_forwarded ok. plain mode')
} else {
    say('check first_x_forwarded fail. plain mode')
}
if req_first_x_forwarded('re:1.1.1.[0-9]') {
    say('check first_x_forwarded ok. regex mode')
} else {
    say('check first_x_forwarded fail. regex mode')
}

For a request with X-Forwarded-For: 1.1.1.1, 10.10.10.10, 172.16.0.1:

req_first_x_forwarded: 1.1.1.1
check first_x_forwarded ok. plain mode
check first_x_forwarded ok. regex mode

req_header

req_header(name String, [pattern String]) → String | Boolean

Returns the value of the specified request header. With pattern, compares the value against match conditions.

ParameterDescription
nameName of the request header. Replace hyphens (-) with underscores (_). For example, use x_user_id for X-USER-ID.
patternMatch conditions (brute-force or re: regex)

Return values without `pattern`:

  • Header exists: returns its value as a string.

  • Header does not exist: returns false.

Return values with `pattern`:

  • Header exists and matches: returns true.

  • Header exists but does not match, or does not exist: returns false.

Example

# req_header
uid = req_header('x_uid')
if uid {
    say(concat('found header x-uid ', uid))
} else {
    say('not found header x-uid')
}
uid_chk = req_header('x_uid', 'es developer')
if uid_chk {
    say('check header x-uid ok. plain mode')
} else {
    say('check header x-uid fail. plain mode')
}
uid_chk = req_header('x_uid', 're:es [a-z]+')
if uid_chk {
    say('check header x-uid ok. regex mode')
} else {
    say('check header x-uid fail. regex mode')
}

For a request with X-UID: es developer:

found header x-uid es developer
check header x-uid ok. plain mode
check header x-uid ok. regex mode