Skip to main content

Referer Restriction

The referer-restriction plugin filters requests according to the host in the Referer header. It can limit a route or service to requests referred by trusted sites or reject traffic referred by known unwanted sites before the request reaches the upstream.

The Referer header is supplied by the client. Browsers can omit it because of referrer policies, and other clients can forge it. Use this plugin to filter traffic associated with referring sites, not to authenticate or strongly authorize clients.

Examples​

The examples show how to allow requests referred by trusted sites and how to block an unwanted referring domain while accepting requests without the header.

Allow Requests from Trusted Sites​

Configure the route to accept an exact host and its subdomains. Requests referred by other hosts are rejected with the default response.

The plugin evaluates the host parsed from the header. An entry that begins with * uses suffix matching, so *.trusted.example.com matches docs.trusted.example.com. A missing or malformed header is rejected by default.

Create the route:

curl "http://127.0.0.1:9180/apisix/admin/routes/referer-restriction-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/anything",
"plugins": {
"referer-restriction": {
"whitelist": [
"trusted.example.com",
"*.trusted.example.com"
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request with a trusted referring host:

curl "http://127.0.0.1:9080/anything" \
-H "Referer: https://docs.trusted.example.com/start" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"

The trusted host reaches the upstream:

Allowed response: 200

Send a request referred by a host that is not on the list:

curl --include "http://127.0.0.1:9080/anything" \
-H "Referer: https://untrusted.example.net/start"

The request is rejected before it reaches the upstream:

HTTP/1.1 403 Forbidden
...
{"message":"Your referer host is not allowed"}

Block Requests from a Referring Domain​

Replace the allowlist with a denylist, customize the rejection message, and allow requests that do not include a Referer header.

curl "http://127.0.0.1:9180/apisix/admin/routes/referer-restriction-route/plugins/referer-restriction" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"blacklist": [
"*.tracker.example.com"
],
"bypass_missing": true,
"message": "referring site blocked"
}'

Send a request from a blocked referring host:

curl --include "http://127.0.0.1:9080/anything" \
-H "Referer: https://ads.tracker.example.com/campaign"

The matching host returns the custom response:

HTTP/1.1 403 Forbidden
...
{"message":"referring site blocked"}

Send a request without the Referer header:

curl "http://127.0.0.1:9080/anything" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"

The request bypasses the restriction and reaches the upstream:

Allowed response: 200