mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Allow NUM_PROXIES=0 and include more docs
This commit is contained in:
parent
d6d4621c45
commit
83da4949c0
|
@ -359,5 +359,11 @@ The name of a parameter in the URL conf that may be used to provide a format suf
|
||||||
|
|
||||||
Default: `'format'`
|
Default: `'format'`
|
||||||
|
|
||||||
|
#### NUM_PROXIES
|
||||||
|
|
||||||
|
An integer of 0 or more, that may be used to specify the number of application proxies that the API runs behind. This allows throttling to more accurately identify client IP addresses. If set to `None` then less strict IP matching will be used by the throttle classes.
|
||||||
|
|
||||||
|
Default: `None`
|
||||||
|
|
||||||
[cite]: http://www.python.org/dev/peps/pep-0020/
|
[cite]: http://www.python.org/dev/peps/pep-0020/
|
||||||
[strftime]: http://docs.python.org/2/library/time.html#time.strftime
|
[strftime]: http://docs.python.org/2/library/time.html#time.strftime
|
||||||
|
|
|
@ -35,16 +35,11 @@ The default throttling policy may be set globally, using the `DEFAULT_THROTTLE_C
|
||||||
'DEFAULT_THROTTLE_RATES': {
|
'DEFAULT_THROTTLE_RATES': {
|
||||||
'anon': '100/day',
|
'anon': '100/day',
|
||||||
'user': '1000/day'
|
'user': '1000/day'
|
||||||
},
|
}
|
||||||
'NUM_PROXIES': 2,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
The rate descriptions used in `DEFAULT_THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period.
|
The rate descriptions used in `DEFAULT_THROTTLE_RATES` may include `second`, `minute`, `hour` or `day` as the throttle period.
|
||||||
|
|
||||||
By default Django REST Framework will try to use the `HTTP_X_FORWARDED_FOR` header to uniquely identify client machines for throttling. If `HTTP_X_FORWARDED_FOR` is not present `REMOTE_ADDR` header value will be used.
|
|
||||||
|
|
||||||
To help Django REST Framework identify unique clients the number of application proxies can be set using `NUM_PROXIES`. This setting will allow the throttle to correctly identify unique requests when there are multiple application side proxies in front of the server. `NUM_PROXIES` should be set to an integer. It is important to understand that if you configure `NUM_PROXIES > 0` all clients behind a unique [NAT'd](http://en.wikipedia.org/wiki/Network_address_translation) gateway will be treated as a single client.
|
|
||||||
|
|
||||||
You can also set the throttling policy on a per-view or per-viewset basis,
|
You can also set the throttling policy on a per-view or per-viewset basis,
|
||||||
using the `APIView` class based views.
|
using the `APIView` class based views.
|
||||||
|
|
||||||
|
@ -71,6 +66,16 @@ Or, if you're using the `@api_view` decorator with function based views.
|
||||||
}
|
}
|
||||||
return Response(content)
|
return Response(content)
|
||||||
|
|
||||||
|
## How clients are identified
|
||||||
|
|
||||||
|
By default the `X-Forwarded-For` HTTP header is used to uniquely identify client machines for throttling. If the `X-Forwarded-For` header is not present, then the value of the `Remote-Addr` header will be used.
|
||||||
|
|
||||||
|
If you need to more strictly identify unique clients, you'll need to configure the number of application proxies that the API runs behind by setting the `NUM_PROXIES` setting. This setting should be an integer of 0 or more, and will allow the throttle to identify the client IP as being the last IP address in the `X-Forwarded-For` header, once any application proxy IP addresses have first been excluded.
|
||||||
|
|
||||||
|
It is important to understand that if you configure the `NUM_PROXIES` setting, then all clients behind a unique [NAT'd](http://en.wikipedia.org/wiki/Network_address_translation) gateway will be treated as a single client.
|
||||||
|
|
||||||
|
Further context on how the `X-Forwarded-For` header works, and identifier a remote client IP can be [found here][identifing-clients].
|
||||||
|
|
||||||
## Setting up the cache
|
## Setting up the cache
|
||||||
|
|
||||||
The throttle classes provided by REST framework use Django's cache backend. You should make sure that you've set appropriate [cache settings][cache-setting]. The default value of `LocMemCache` backend should be okay for simple setups. See Django's [cache documentation][cache-docs] for more details.
|
The throttle classes provided by REST framework use Django's cache backend. You should make sure that you've set appropriate [cache settings][cache-setting]. The default value of `LocMemCache` backend should be okay for simple setups. See Django's [cache documentation][cache-docs] for more details.
|
||||||
|
@ -183,5 +188,6 @@ The following is an example of a rate throttle, that will randomly throttle 1 in
|
||||||
|
|
||||||
[cite]: https://dev.twitter.com/docs/error-codes-responses
|
[cite]: https://dev.twitter.com/docs/error-codes-responses
|
||||||
[permissions]: permissions.md
|
[permissions]: permissions.md
|
||||||
|
[identifing-clients]: http://oxpedia.org/wiki/index.php?title=AppSuite:Grizzly#Multiple_Proxies_in_front_of_the_cluster
|
||||||
[cache-setting]: https://docs.djangoproject.com/en/dev/ref/settings/#caches
|
[cache-setting]: https://docs.djangoproject.com/en/dev/ref/settings/#caches
|
||||||
[cache-docs]: https://docs.djangoproject.com/en/dev/topics/cache/#setting-up-the-cache
|
[cache-docs]: https://docs.djangoproject.com/en/dev/topics/cache/#setting-up-the-cache
|
||||||
|
|
|
@ -28,8 +28,12 @@ class BaseThrottle(object):
|
||||||
remote_addr = request.META.get('REMOTE_ADDR')
|
remote_addr = request.META.get('REMOTE_ADDR')
|
||||||
num_proxies = api_settings.NUM_PROXIES
|
num_proxies = api_settings.NUM_PROXIES
|
||||||
|
|
||||||
if xff and num_proxies:
|
if num_proxies is not None:
|
||||||
return xff.split(',')[-min(num_proxies, len(xff))].strip()
|
if num_proxies == 0 or xff is None:
|
||||||
|
return remote_addr
|
||||||
|
addrs = xff.split(',')
|
||||||
|
client_addr = addrs[-min(num_proxies, len(xff))]
|
||||||
|
return client_addr.strip()
|
||||||
|
|
||||||
return xff if xff else remote_addr
|
return xff if xff else remote_addr
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user