All Products
Search
Document Center

CDN:Request logic functions

Last Updated:Jun 29, 2026

These functions inspect properties of the current request — server address, client identity, URI components, headers, cookies, and geographic data. Use them in EdgeScript to build conditional logic for routing, access control, and content customization.

Important

Functions that return client geographic data (client_addr, client_country, client_region, client_isp) may return inaccurate values. Client addresses can be rewritten by Internet service provider (ISP) NAT rules, or the IP address may fall outside the Alibaba Cloud CDN address pool.

server_addr

Returns the IP address of the CDN node that received the current request.

Syntax

server_addr()

Parameters

None.

Return value

The server's IP address. Data type: string.

Example

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

server_port

Returns the port on the CDN node that received the current request.

Syntax

server_port()

Parameters

None.

Return value

The server port. Data type: numeric.

Example

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

client_addr

Returns the IP address of the client that sent the request.

Return values may not be accurate. See the accuracy note at the top of this page.

Syntax

client_addr()

Parameters

None.

Return value

The client's IP address. Data type: string.

Example

c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))

client_port

Returns the port of the client that sent the request.

Syntax

client_port()

Parameters

None.

Return value

The client port. Data type: numeric.

Example

c_addr = client_addr()
c_port = client_port()
say(concat('c_addr:', c_addr))
say(concat('c_port:', tostring(c_port)))

client_country

Returns the country code of the client. Use with client_region() and client_isp() to build geo-based logic.

Return values may not be accurate. See the accuracy note at the top of this page.

Syntax

client_country()

Parameters

None.

Return value

The client's country code. Data type: string. For country code values, see Country codes.

Example

The following example retrieves the country, administrative division, and ISP of the client and prints each value if it is set.

c_country = client_country()
c_region = client_region()
c_isp = client_isp()
if c_country {
    say(concat('client_country:', c_country))
}
if c_region {
    say(concat('client_region:', c_region))
}
if c_isp {
    say(concat('client_isp:', c_isp))
}

client_region

Returns the administrative division code of the client's location.

Return values may not be accurate. See the accuracy note at the top of this page.

Syntax

client_region()

Parameters

None.

Return value

The client's administrative division code. Data type: string. For code values, see Administrative division codes.

Example

See the example under client_country.

client_isp

Returns the ISP code of the client's network.

Return values may not be accurate. See the accuracy note at the top of this page.

Syntax

client_isp()

Parameters

None.

Return value

The client's ISP code. Data type: string. For code values, see ISP codes.

ip_region

Returns the administrative division code of the city or province that a given IP address belongs to.

Syntax

ip_region(ipaddr)

Parameters

Parameter

Description

ipaddr

An IP address in dotted decimal notation, for example, 192.168.0.1.

Return value

The administrative division code of the city or province. Data type: string. For code values, see Administrative division codes.

ip_isp

Returns the ISP code for a given IP address.

Syntax

ip_isp(ipaddr)

Parameters

Parameter

Description

ipaddr

An IP address in dotted decimal notation, for example, 192.168.0.1.

Return value

The ISP code of the IP address. Data type: string. For code values, see ISP codes.

req_uri

Returns the URI of the request, excluding query parameters. When called with a pattern argument, matches the URI against the pattern instead.

Syntax

req_uri([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the request URI. Data type: string.

  • With pattern: true if the URI matches, false otherwise.

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 the request GET /path1/path2?mode=ip, the output is:

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

req_uri_basename

Returns the filename in the request URI, without the file extension. When called with a pattern argument, matches the filename against the pattern instead.

The filename is the last path component stripped of its extension. For example:

  • /document_detail/30360.html30360

  • /M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp42-1_g

  • /tarball/foo.tar.bz2foo

Syntax

req_uri_basename([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the filename. Data type: string.

  • With pattern: true if the filename matches, false otherwise.

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 the request GET /path1/path2/foo.tar.bz2, the output is:

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

req_uri_ext

Returns the file extension in the request URI. When called with a pattern argument, matches the extension against the pattern instead.

The extension includes the leading dot. For multi-part extensions, the full suffix is returned. For example:

  • /document_detail/30360.html.html

  • /M604/guopei_mp4/ZYJY2017BJGL0101/2-1_g.mp4.mp4

  • /tarball/foo.tar.bz2.tar.bz2

Syntax

req_uri_ext([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the file extension. Data type: string.

  • With pattern: true if the extension matches, false otherwise.

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 the request GET /path1/path2/foo.tar.bz2, the output is:

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

req_uri_seg

Returns URI path segments as a dictionary, keyed by 1-based index from left to right.

  • Without idx: returns all segments.

  • With idx: returns segments starting from that index (inclusive).

Each segment can contain up to 128 characters. Characters beyond this limit are dropped.

Syntax

req_uri_seg([idx])

Parameters

Parameter

Required

Description

idx

No

The start index. Indexes begin at 1.

Return value

A dictionary of path segments keyed by index. Check each key before accessing it, as missing keys return 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 the request GET /path1/path2/path3/path4?mode=req2, the output is:

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(3): segs[4]=path4

req_uri_arg

Returns the value of a named query parameter. When called with a pattern argument, matches the parameter value against the pattern instead.

Syntax

req_uri_arg(name, [pattern])

Parameters

Parameter

Required

Description

name

Yes

The name of the query parameter.

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern:

    • Parameter exists: the parameter value. Data type: string.

    • Parameter not found: false.

  • With pattern:

    • Parameter exists and value matches: true.

    • Parameter exists but value does not match: false.

    • Parameter not found: 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')
}

The output varies based on the request:

Request

Output

/path1/path2/path3/path4?mode=req4&uid

not found uid / check uid fail. plain mode / check uid fail. regex mode

/path1/path2/path3/path4?mode=req4&uid=

found uid / check uid fail. plain mode / check uid fail. regex mode

/path1/path2/path3/path4?mode=req4&uid=12345

found uid 12345 / check uid fail. plain mode / check uid ok. regex mode

req_uri_query_string

Returns the full query string of the request, excluding the leading ?. When called with a pattern argument, matches the query string against the pattern instead.

Syntax

req_uri_query_string([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the query string. Data type: string.

  • With pattern: true if the query string matches, false otherwise.

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 the request GET /path1/path2/path3/path4?mode=req5&token=34Deasd#243, the output is:

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

req_scheme

Returns the scheme of the request (http or https). When called with a pattern argument, matches the scheme against the pattern instead.

Syntax

req_scheme([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the request scheme. Data type: string.

  • With pattern: true if the scheme matches, false otherwise.

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 an HTTP request, the output is:

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

req_method

Returns the HTTP method of the request. When called with a pattern argument, matches the method against the pattern instead.

Syntax

req_method([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the HTTP method. Data type: string.

  • With pattern: true if the method matches, false otherwise.

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 request, the output is:

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

req_host

Returns the value of the Host request header. When called with a pattern argument, matches the host against the pattern instead.

Syntax

req_host([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the Host header value. Data type: string.

  • With pattern: true if the host matches, false otherwise.

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 the request Host: image.developer.aliyundoc.com, the output is:

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

req_user_agent

Returns the value of the User-Agent request header. When called with a pattern argument, matches the value against the pattern instead.

Syntax

req_user_agent([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the User-Agent header value. Data type: string.

  • With pattern: true if the value matches, false otherwise.

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 the request User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64), the output is:

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

Returns the value of the Referer request header. When called with a pattern argument, matches the value against the pattern instead.

Syntax

req_referer([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the Referer header value. Data type: string.

  • With pattern: true if the value matches, false otherwise.

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 the request Referer: https://example.aliyundoc.com/******00003, the output is:

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

req_cookie

Returns the value of a named cookie. When called with a pattern argument, matches the cookie value against the pattern instead.

Syntax

req_cookie(name, [pattern])

Parameters

Parameter

Required

Description

name

Yes

The name of the cookie.

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern:

    • Cookie exists: the cookie value. Data type: string.

    • Cookie not found: false.

  • With pattern:

    • Cookie exists and value matches: true.

    • Cookie exists but value does not match: false.

    • Cookie not found: 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 the request Cookie: uid=123456; token=value2, the output is:

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

req_first_x_forwarded_addr

Returns the first IP address in the X-Forwarded-For request header. When called with a pattern argument, matches the address against the pattern instead.

Syntax

req_first_x_forwarded_addr([pattern])

Parameters

Parameter

Required

Description

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern: the first IP address in the X-Forwarded-For header. Data type: string.

  • With pattern: true if the address matches, false otherwise.

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 the request X-Forwarded-For: 1.1.X.X, 10.10.10.10, 172.16.0.1, the output is:

req_first_x_forwarded: 1.1.X.X
check first_x_forwarded ok. plain mode
check first_x_forwarded ok. regex mode

req_header

Returns the value of a named request header. When called with a pattern argument, matches the header value against the pattern instead.

Syntax

req_header(name, [pattern])

Parameters

Parameter

Required

Description

name

Yes

The header name with hyphens replaced by underscores, in lowercase. For example, X-USER-ID becomes x_user_id.

pattern

No

A match pattern. Two match types are supported: exact match (default) and regular expression (prefix the pattern with re:).

Return value

  • Without pattern:

    • Header exists: the header value. Data type: string.

    • Header not found: false.

  • With pattern:

    • Header exists and value matches: true.

    • Header exists but value does not match: false.

    • Header not found: 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 the request X-UID: es developer, the output is:

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

req_id

Returns the Eagle Eye ID of the request. Each Eagle Eye ID uniquely identifies a single request.

Syntax

req_id()

Parameters

None.

Return value

The request ID. Data type: string.

Example

# req_id
say(concat('req_id: ', req_id()))

Example output: req_id: 6451c43d15815890089411000e