实现前奏

目标:按照指定的参数(如分类/商品编号)做一致性hash,从而保证相同数据到一台机器上
先说下nginx里$request_uri和$uri的区别
$request_uri
This variable is equal to the original request URI as received from the client including the args. It cannot be modified. Look at $uri for the post-rewrite/altered URI. Does not include host name. Example: "/foo/bar.php?arg=baz"
这个变量等于从客户端发送来的原生请求URI,包括参数。它不可以进行修改。$uri变量反映的是重写后/改变的URI。不包括主机名。例如:"/foo/bar.php?arg=baz"
$uri
This variable is the current request URI, without any arguments (see $args for those). This variable will reflect any modifications done so far by internal redirects or the index module. Note this may be different from $request_uri, as $request_uri is what was originally sent by the browser before any such modifications. Does not include the protocol or host name. Example: /foo/bar.html

这个变量指当前的请求URI,不包括任何参数(见$args)。这个变量反映任何内部重定向或index模块所做的修改。注意,这和$request_uri不同,因$request_uri是浏览器发起的不做任何修改的原生URI。不包括协议及主机名。例如:"/foo/bar.html"
$document_uri
The same as $uri.
同$uri.

实现步骤

第一步nginx配置

在nginx中做简单配置,先实现轮训,域名可以根据自己的来

server {
    listen       80;
    server_name  default www.test.com;
    location / {
        proxy_pass http://www_posts;
        index index.html index.htm;
    }
}
upstream www_posts {
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}
server {
    listen       7001;
    server_name  default www.test.com;
    location / {
        root   /data/www/post1/;
        index index.html index.htm;
    }
}
server {
    listen       7002;
    server_name  default www.test.com;
    location / {
        root   /data/www/post2/;
        index index.html index.htm;
    }
}
server {
    listen       7003;
    server_name  default www.test.com;
    location / {
        root   /data/www/post3/;
        index index.html index.htm;
    }
}

完成后 重启nginx

第二步创建程序文件

mkdir /data/www/post1; cd /data/www/post1;
vim 1.html //写入 1-1
vim 2.html //写入 1-2
vim 3.html //写入 1-3
mkdir /data/www/post2; cd /data/www/post2;
vim 1.html //写入 2-1
vim 2.html //写入 2-2
vim 3.html //写入 2-3
mkdir /data/www/post3; cd /data/www/post3;
vim 1.html //写入 3-1
vim 2.html //写入 4-2
vim 3.html //写入 3-3
通过浏览器访问 www.test.com/1.html,不断刷新,可分别出现1-1,2-1,3-1

第三步自定义hash key

在server-location中加入

if ( $uri ~* "^\/(\d+).html$" ){ set $defurlkey $1; }

修改upstream,结果如下

upstream www_posts {
 hash $defurlkey;
    server 127.0.0.1:7001;
    server 127.0.0.1:7002;
    server 127.0.0.1:7003;
}

可通过分别访问,测试结果
www.test.com/1.html
www.test.com/2.html
www.test.com/3.html