-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
79 lines (59 loc) · 2.98 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Nginx Upstream Ketama-based Consistent Replicated Balancer
--
Description:
--
This module uses ketama algorithm to consistently hash requested URI's to backend servers (given that list of servers don't change).
Ketama was originally developed for mapping keys to servers in a memcache pool.
Its explanation could be found here: http://www.lastfm.ru/user/RJ/journal/2007/04/10/rz_libketama_-_a_consistent_hashing_algo_for_memcache_clients
There is already exists the module that uses ketama exactly for memcache upstreams: https://github.com/dctrwatson/nginx-upstream-consistent
Using it you could be sure that your backends and Nginx maps keys in the same way.
But it works only with memcached_pass (key is taken directly from request).
This module is more universal - key to hash is passed via variable. Another major difference is that each key could be hashed to few backends (hashing to 1 backend is possible, in that case it will be compatible with memcached implementation).
Same key always will be mapped to the same backends (again given that list of servers don't change).
Mapping to a few backends may be useful, for example, to achieve high-availability.
Since Nginx upstream modules can't return few selected backends (which could then be used in something like try_files) this module chooses one of mapped backends randomly. But together with proxy_next_upstream this could be configured to try all backends in turn until we get a successful response.
Installation:
--
You'll need to re-compile Nginx from source to include this module.
Modify your compile of Nginx by adding the following directive
(modified to suit your path of course):
./configure --with-http_ssl_module --add-module=/absolute/path/to/nginx-upstream-consistent-replicated
make
make install
Usage:
--
Change your Nginx config file upstream block to include the 'consistent_replicated' directive:
upstream backend {
consistent_replicated;
server 192.168.0.2:9000;
server 192.168.0.3:9000;
server 192.168.0.4:9000;
}
Also it may accept parameters ketama_points, weight_scale and replication_level.
By default they are respectively 150, 10 and 1 and thus should be compatible with Perl's Cache::Memcached::Fast module.
upstream backend {
consistent_replicated ketama_points=150 weight_scale=10 replication_level=1;
server 192.168.0.2:9000;
server 192.168.0.3:9000;
server 192.168.0.4:9000;
}
Usage with proxy_next_upstream:
upstream backend {
consistent_replicated ketama_points=150 weight_scale=10 replication_level=2;
server 192.168.0.2:9000;
server 192.168.0.3:9000;
server 192.168.0.4:9000;
}
server {
...
location /storage {
try_files $uri @next;
}
location @next {
proxy_set_header Host $host;
proxy_connect_timeout 3s;
proxy_read_timeout 3s;
proxy_next_upstream error timeout invalid_header http_404 http_500 http_502 http_503 http_504;
proxy_pass http://backend;
}
}