您的位置:首页 > 数据库 > Memcache

nginx连接memcached

2015-12-07 20:49 686 查看
一.nginx关联上php的配置:

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include
fastcgi_params;

}

配置后重启nignx: xxxxxx/nginx/sbin/nginx -s reload;

测试test.php能否正常运行。

二.php编译完成的情况下,如何在单独编译一个php的扩展,如memcache

1.进入下载解压好的memcache目录:cd memcache-2.2.7

2.看你针对哪个php来给它编译memcache,执行:

/usr/local/php/bin/phpize

3.执行完后,当前memcache-2.2.7下,就多了 很多文件(configure配置文件)

4.然后执行:./configure --with-php-config=/usr/local/php/bin/php-config

5.执行: make && make install

6.编译成功最后一行会提示:Installing shared extensions: (这个后面的地址有用)

7.打开php.ini加一句:

extension = (上面的地址)/memcache.so

9. 重启php(nginx的php的独立进程):

在php目录下执行:

pkill -9 php

./sbin/php-fpm

10.phpinfo查看是否有memcache扩展(成功)

三、nginx和memcached的直接连接

nginx.conf里:

location / {

set $memcached_key "$uri";

memcached_pass 127.0.0.1:11211;

error_page 404 /callback.php;

}

在callback.php里连接memcache,把没有查到的key,php从数据库查询之后再写入memcache:

<?php

$uri = $_SERVER['REQUEST_URI']; //接收用户查询的key

//连接数据库做查询...

//写入memcache

$mem = new memcache();

$mem->connect('localhost',11211);

$mem->add($url,'value',0,300);

$mem->close();

?>

思考:如果有多台memcahced服务器,某个key去请求哪个?php又帮nginx把内容存储到哪个memcached服务器?

多台memcached时,nginx与php,如果保持集群上的算法的同步?

1.要有稳定的算法

2.nginx和php对于memcached的算法要同步
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: