最近在开发的时候,发现以前APP客户端的一部分页面用的是webview交互,这些页面请求不少,打开一套试卷,将会产生100+的请求量,致使系统性能降低。因而考虑在最靠近客户端的Nginx服务器上作Redis缓存。综合了下网上对于php缓存的资料,通过一番改动,终于搭建成功。因为网上的是针对php的,并且没有说明,对于我这种彻底不动运维的人来讲,研究下来仍是挺痛苦的。因此整理一份比较完整的,供你们参考。php
如下的配置中,可能有不适合或者写的有问题的。请留言指出,谢谢!html
最终缓存之后,整个项目结构图以下(图片复制的,请自动脑补充memcache为redis,php为tomcat):nginx
参考文章地址:git
1.srcache_nginx+redis构建缓存系统 http://www.ttlsa.com/nginx/construction-of-srcache_nginx_redis-caching-system/github
2.httpsrcachemodule wiki http://wiki.nginx.org/HttpSRCacheModule#srcache_response_cache_controlweb
首先下载Nginx安装包,tar zvxf解压到/usr/local/src目录;redis
下载模块ngx_devel_kit, set-misk-nginx-module, srcache-nginx-module, echo-nginx-module, ngx-http-redis, redis2-nginx-module;apache
将这些模块解压到/usr/local/src/modules/下面;缓存
进入/usr/local/src/nginx-1.8.0/目录,执行以下命令:tomcat
./configure --add-module=../modules/echo-nginx-module-0.57 --add-module=../modules/ngx_http_redis-0.3.7 --add-module=../modules/ngx_devel_kit-0.2.19 --add-module=../modules/set-misc-nginx-module-0.29 --add-module=../modules/srcache-nginx-module-master --add-module=../modules/redis2-nginx-module-master
而后执行make;make install;
默认安装到/usr/local/nginx/目录中,至此安装成功;
首先在Http体中声明upstream(这个命令没有研究,只能本身猜想了下),代码以下:
upstream redis{ server 127.0.0.1:6379; keepalive 512; }
server 是Redis服务器的IP+PORT,keepalive是保持的链接数,这个链接数是网上的,对于个人项目来讲应该是太大了。你们自行修改。
配置Server中的location监听
location /test/ { #这三个命令参考srcache 文档,http://wiki.nginx.org/HttpSRCacheModule srcache_store_private on; srcache_methods GET; srcache_response_cache_control off; #匹配本身的路径,因为Nginx不支持嵌套if,因此这么写 if ($uri ~ /test/index\.jsp$){ set $flag "${flag}1"; } if ($arg_id ~ [0-9]+$){ set $flag "${flag}1"; } if ($flag = "011"){ #这里我用普通的请求参数来做为缓存的键值,网上的是用MD5,可是对于更新缓存又多了操做。你们根据业务自行调整。 set $key $arg_id; set_escape_uri $escaped_key $key; #请求过来会先查询这个 srcache_fetch GET /redis $key; #过时时间 srcache_default_expire 60; srcache_store PUT /redis2 key=$escaped_key&exptime=$srcache_expire; #添加头信息 add_header X-Cached-From $srcache_fetch_status; add_header X-Cached-Store $srcache_store_status; add_header X-Key $key; set_md5 $md5key $key; add_header X-md5-key $md5key; add_header X-Query_String $query_string; add_header X-expire $srcache_expire; } #网上都是用fast_cgi来代理,没弄会,就用最初的了,貌似fast_cgi是apache php下用的 proxy_pass http://192.168.1.102:8080; } #redis模块 location = /redis { internal; set $redis_key $args; redis_pass redis; } #redis2模块 location = /redis2 { internal; set_unescape_uri $exptime $arg_exptime; set_unescape_uri $key $arg_key; redis2_query set $key $echo_request_body; redis2_query expire $key $exptime; redis2_pass redis; }
到这里后,配置就完成了。
响应头信息
运行效果明显的是X-cached-from这个头信息的变化。
不明白用了httpredis2为何还要引入httpredis,查询文档后的结果是说redis2是httpredis升级版,可是wiki上说:
Also, you need both HttpRedisModule and HttpRedis2Module. The former is used in the srcache_fetch subrequest and the latter is used in the srcache_store subrequest.
也就是说两个都要。
另外就是对于/redis这个location里,没有redis get这样的代码,是怎么获取到返回信息的。最初本身只安装httpredsi2,用redis2_pass redis很差使。而后加入redis2_query get $redis_key,依然很差使。无奈只好按照文档上的来了。
有比较熟悉这个的大侠请留言指出,好在部署线上环境前进行优化!!谢谢!!
http://www.cnblogs.com/luochengqiuse/p/4677027.html