Upstream
Although Upstream can be directly configured to the or Service, using an Upstream object is recommended when there is duplication as shown below.
By creating an Upstream object and referencing it by in the Route, you can ensure that there is only a single value of the object that needs to be maintained.
An Upstream configuration can be directly bound to a Route or a Service, but the configuration in Route has a higher priority. This behavior is consistent with priority followed by the object.
In addition to the equalization algorithm selections, Upstream also supports passive health check and retry for the upstream. You can learn more about this .
After creating an Upstream object, it can be referenced by a specific Route or Service as shown below.
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/index.html",
"upstream_id": 1
}'
For convenience, you can directly bind the upstream address to a Route or Service.
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/index.html",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"rejected_code": 503,
"key": "remote_addr"
}
},
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
The example below shows how you can configure a health check.
You can learn more about health checks .
The examples below show configurations that use different hash_on
types.
curl http://127.0.0.1:9180/apisix/admin/consumers \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"username": "jack",
"plugins": {
"key-auth": {
"key": "auth-jack"
}
}
}'
Creating a Route object and enabling the key-auth
authentication Plugin.
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"key-auth": {}
},
"upstream": {
"nodes": {
"127.0.0.1:1981": 1
},
"type": "chash",
},
"uri": "/server_port"
}'
To test the request, the consumer_name
passed for authentication will be used as the hash value of the load balancing hash algorithm.
Creating a Route and an upstream object.
curl http://127.0.0.1:9180/apisix/admin/routes/1 \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hash_on_cookie",
"upstream": {
"key": "sid",
"type": "chash",
"hash_on": "cookie",
"nodes": {
"127.0.0.1:1980": 1,
"127.0.0.1:1981": 1
}
}
}'
The client can then send a request with a cookie.
curl http://127.0.0.1:9080/hash_on_cookie \
-H "Cookie: sid=3c183a30cffcda1408daf1c61d47b274"
Creating a Route and an upstream object.
curl http://127.0.0.1:9080/hash_on_header \
-H "Content-Type: application/json"