You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/latest/plugins/ip-restriction.md
+72-82
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ keywords:
6
6
- Plugin
7
7
- IP restriction
8
8
- ip-restriction
9
-
description: This document contains information about the Apache APISIX ip-restriction Plugin.
9
+
description: The ip-restriction Plugin supports restricting access to upstream resources by IP addresses, through either configuring a whitelist or blacklist of IP addresses.
10
10
---
11
11
12
12
<!--
@@ -28,11 +28,13 @@ description: This document contains information about the Apache APISIX ip-restr
The `ip-restriction` Plugin allows you to restrict access to a Service or a Route by either whitelisting or blacklisting IP addresses.
35
+
## Description
34
36
35
-
Single IPs, multiple IPs or even IP ranges in CIDR notation like `10.10.10.0/24` can be used.
37
+
The `ip-restriction` Plugin supports restricting access to upstream resources by IP addresses, through either configuring a whitelist or blacklist of IP addresses. Restricting IP to resources helps prevent unauthorized access and harden API security.
36
38
37
39
## Attributes
38
40
@@ -45,15 +47,16 @@ Single IPs, multiple IPs or even IP ranges in CIDR notation like `10.10.10.0/24`
45
47
46
48
:::note
47
49
48
-
Either one of `whitelist` or `blacklist`attribute must be specified. They cannot be used together.
50
+
At least one of the `whitelist` or `blacklist`should be configured, but they cannot be configured at the same time.
49
51
50
52
:::
51
53
52
-
## Enable Plugin
54
+
## Examples
53
55
54
-
You can enable the Plugin on a Route or a Service as shown below:
56
+
The examples below demonstrate how you can configure the `ip-restriction` Plugin for different scenarios.
55
57
56
58
:::note
59
+
57
60
You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command:
58
61
59
62
```bash
@@ -62,103 +65,90 @@ admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"/
62
65
63
66
:::
64
67
65
-
```shell
66
-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
67
-
{
68
-
"uri": "/index.html",
69
-
"upstream": {
70
-
"type": "roundrobin",
71
-
"nodes": {
72
-
"127.0.0.1:1980": 1
73
-
}
74
-
},
75
-
"plugins": {
76
-
"ip-restriction": {
77
-
"whitelist": [
78
-
"127.0.0.1",
79
-
"113.74.26.106/24"
80
-
]
81
-
}
82
-
}
83
-
}'
84
-
```
68
+
### Restrict Access by Whitelisting
85
69
86
-
To return a custom message when an IP address is not allowed access, configure it in the Plugin as shown below:
70
+
The following example demonstrates how you can whitelist a list of IP addresses that should have access to the upstream resource and customize the error message for access denial.
87
71
88
-
```json
89
-
"plugins": {
90
-
"ip-restriction": {
72
+
Create a Route with the `ip-restriction` Plugin to whitelist a range of IPs and customize the error message when the access is denied:
73
+
74
+
```shell
75
+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
76
+
-H "X-API-KEY: ${admin_key}" \
77
+
-d '{
78
+
"id": "ip-restriction-route",
79
+
"uri": "/anything",
80
+
"plugins": {
81
+
"ip-restriction": {
91
82
"whitelist": [
92
-
"127.0.0.1",
93
-
"113.74.26.106/24"
83
+
"192.168.0.1/24"
94
84
],
95
-
"message": "Do you want to do something bad?"
85
+
"message": "Access denied"
86
+
}
87
+
},
88
+
"upstream": {
89
+
"type": "roundrobin",
90
+
"nodes": {
91
+
"httpbin.org:80": 1
92
+
}
96
93
}
97
-
}
94
+
}'
98
95
```
99
96
100
-
## Example usage
101
-
102
-
After you have configured the Plugin as shown above, when you make a request from the IP `127.0.0.1`:
103
-
104
-
```shell
105
-
curl http://127.0.0.1:9080/index.html -i
106
-
```
97
+
Send a request to the Route:
107
98
108
99
```shell
109
-
HTTP/1.1 200 OK
110
-
...
100
+
curl -i "http://127.0.0.1:9080/anything"
111
101
```
112
102
113
-
But if you make requests from `127.0.0.2`:
103
+
If your IP is allowed, you should receive an `HTTP/1.1 200 OK` response. If not, you should receive an `HTTP/1.1 403 Forbidden` response with the following error message:
The following example demonstrates how you can modify the IP used for IP restriction, using the `real-ip` Plugin. This is particularly useful if APISIX is behind a reverse proxy and the real client IP is not available to APISIX.
124
112
125
-
To change the whitelisted/blacklisted IPs, you can update the Plugin configuration. The changes are hot reloaded and there is no need to restart the service.
113
+
Create a Route with the `ip-restriction`Plugin to whitelist a specific IP address and obtain client IP address from the URL parameter `realip`:
126
114
127
115
```shell
128
-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
129
-
{
130
-
"uri": "/index.html",
131
-
"upstream": {
132
-
"type": "roundrobin",
133
-
"nodes": {
134
-
"127.0.0.1:1980": 1
135
-
}
136
-
},
116
+
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
To remove the `ip-restriction` Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.
148
+
Send another request with a different IP address:
151
149
152
150
```shell
153
-
curl http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
0 commit comments