Nginx and PHP using a consistent hashing algorithm for memcached cluster and load balancing

Source: Internet
Author: User
Tags fpm memcached php write pkill


First, you need to compile a third party module (ngx_http_consistent_hash) for Nginx 1. Download Ngx_http_consistent_hash-master, unzip
2. View installed Nginx compilation parameters:/usr/local/nginx/sbin/nginx-v
3. Nginx Decompression directory to perform:./configure--prefix=/usr/local/nginx--add_module=/usr/local/src/ngx_http_consistent_ hash-master/
(/usr/local/nginx: Original nginx compiled path, usr/local/src/ngx_http_consistent_hash-master/: Third party module directory path)
4. Because compiled Nginx may be running: pkill-9 nginx
5. Make && make install


II. Configuration of Memcache cluster
1. Use the memcached node and declare it in a group
Upstream Memserver {
Hash_key $request _uri; #hash计算时的依据, based on the URI to hash
Server localhost:11211;
Server localhost:11212;
Server localhost:11213;
}
2.location in
Location/{
Set $memcached _key $uri;
Memcached_pass Memserver; #memserver为上面的memcache节点的名称
Error_page 404/writemem.php;
}


3. Restart Nginx:./sbin/nginx-s Reload


4. Open the 3 memcache servers:
/usr/local/memcached/bin/memcached-u nobody-vv-p 11211
/usr/local/memcached/bin/memcached-u nobody-vv-p 11212
/usr/local/memcached/bin/memcached-u nobody-vv-p 11213




5.
The above is the default load balancing algorithm: Set the counter, turn to request N servers.
We want to use the installed third party module: (Top installed)
such as Http://wiki.nginx.org/NginxHttpUpstreamConsistentHash
This module is to use a consistent hash to request the backend node, and its algorithm, in PHP with the Memcache module of the consistent hash algorithm, compatible.
After the module is installed, in nginx.conf:
Upstream Memserver {
Consistent_hash $request _uri;
Server 127.0.0.1:11211;
Server 127.0.0.1:11212;
Server 127.0.0.1:11213;
}


6. In php.ini, the following configuration: Memcache.hash_strategy = consistent
In this way: Nginx and PHP can complete the memcached cluster and load balancing algorithm.


Kill PHP Process: pkill-9 php-fpm
Start PHP:/USR/LOCAL/PHP/SBIN/PHP-FPM


7.writemem.php Write Memcache operations:
<?php
$uri = $_server[' Request_uri ']; Key to receive user queries


PHP also wants to add as many servers as Nginx
$mem = new Memcache ();
$mem->addserver (' 127.0.0.1 ', 11211);
$mem->addserver (' 127.0.0.1 ', 11212);
$mem->addserver (' 127.0.0.1 ', 11213);


Querying the database
Todo...


Write to Memcached
$mem->add ($url, ' value ', 0,300);
$mem->close ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.