nginx连接memchached

http://blog.cnwyhx.com/centos-linux-nginx-php-install-v6/
root@localhost nginx]# ./sbin/nginx
location ~ \.php$ {
 #      proxy_pass http://192.168.88.206:8080;
          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;
}
wget http://pecl.php.net/get/memcache-2.2.7.tgz
[root@localhost nginx]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
已安装好memchache
编译扩展
[root@localhost memcache-3.0.7]# /usr/local/fastphp/bin/phpize
[root@localhost memcache-3.0.7]# ./configure --help|grep php
  --with-php-config=PATH  Path to php-config php-config

  [root@localhost memcache-3.0.7]# ./configure --with-php-config=/usr/local/fastphp/bin/php-config
[root@localhost memcache-3.0.7]# make &&make install
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/fastphp/lib/php/extensions/no-debug-non-zts-20100525/
[root@localhost memcache-3.0.7]# cd /usr/local/fastphp/
[root@localhost fastphp]# vim lib/php.ini
extension=/usr/local/fastphp/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
[root@localhost fastphp]# pkill -9 php
oot@localhost fastphp]# ./sbin/php-fpm
[root@localhost fastphp]# cd /usr/local/nginx
[root@localhost nginx]# vi html/test.php
<?php
phpinfo();
//var_dump(mysql_connect('localhost','root',''));
echo rand(1,1000);
print_r($_SERVER);
?>
[root@localhost nginx]# ./sbin/nginx

http://168.88.156/test.php
出现下面
memcache
memcache support    enabled
Version     3.0.7
Revision     $Revision: 326387 $

Directive    Local Value    Master Value
memcache.allow_failover    1    1
memcache.chunk_size    32768    32768
memcache.compress_threshold    20000    20000
memcache.default_port    11211    11211
memcache.hash_function    crc32    crc32
memcache.hash_strategy    consistent    consistent
memcache.lock_timeout    15    15
memcache.max_failover_attempts    20    20
memcache.protocol    ascii    ascii
memcache.redundancy    1    1
memcache.session_redundancy    2    2
思路nginx要设定key 去查memchace 若有就返回
location /{
     set $memcached_key '$uri'          #"$uri?args"
     memcached_pass 127.0.0.1:11211;
}

[root@localhost soft]# /usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1  -p  11211 -c 256 -P /tmp/memcached.pid
[root@localhost soft]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Escape character is '^]'.
add user1.html 0 0 7
iamlisi
STORED
add /user1.html 0 0 7
iamlisi
STORED
[root@localhost nginx]# ./sbin/nginx -s reload
http://192.168.88.156/user1.html
[root@localhost nginx]# vi conf/nginx.conf
location /{
     set $memcached_key "$uri"           #"$uri?args"
     memcached_pass 127.0.0.1:11211;
     error_page 404 /callack.php;
}

[root@localhost nginx]# vi html/callack.php
<?php
print_r($_SERVER);
http://192.168.88.156/user2.html
[root@localhost nginx]# vi html/callack.php
<?php
$uri=$_SERVER['REQUEST_URI'];
$uid=substr($uri,5,strpos($uri,'.')-5);
$conn=mysql_connect('localhost','root','');
$sql='use test';
mysql_query($sql,$conn);
$sql='set names utf8';
mysql_query($sql,$conn);
$sql='select * from user where uid='.$uid;
$rs=mysql_query($sql,$conn);
$user =mysql_fetch_assoc($rs);
if(empty($user)){
echo 'no this user '
}else{
print_r($user);
$mem=new memcache();
$mem->connect('localhost',11211);
$mem->add($uri,$user,0,300);
}
[root@localhost ~]# /etc/rc.d/init.d/mysqld start
mysql> select * from user;
+------+----------+
| id   | uname    |
+------+----------+
|    2 | zhangsan |
|    4 | lisi     |
|    6 | wangwu   |
|    8 | zhaoliu  |
+------+----------+
4 rows in set (0.00 sec)



你可能感兴趣的:(nginx连接memchached)