nginx+lua+redis三国演义
发布于: 2016-10-28 15:11分类: linux,nginx,lua
今天看到redis2_query redis2_pass,然后网上一看,原来又是nginx的扩展。
以下copy 网路,自己还没有真正搞过。
redis2-nginx-module 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。
安装lua # apt-get install lua5.1 # apt-get install liblua5.1-dev # apt-get install liblua5.1-socket2 安装nginx # apt-get install git-core # git clone https://github.com/simpl/ngx_devel_kit.git # git clone https://github.com/chaoslawful/lua-nginx-module.git # git clone https://github.com/agentzh/redis2-nginx-module.git # git clone https://github.com/agentzh/set-misc-nginx-module.git # git clone https://github.com/agentzh/echo-nginx-module.git # git clone https://github.com/catap/ngx_http_upstream_keepalive.git # apt-get install libpcre3 libpcre3-dev libltdl-dev libssl-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libxml2-dev libcurl4-openssl-dev libmcrypt-dev autoconf libxslt1-dev libgd2-noxpm-dev libgeoip-dev libperl-dev -y # wget http://nginx.org/download/nginx-1.0.8.tar.gz # tar zxvf nginx-1.0.8.tar.gz # cd nginx-1.0.8 # ./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \ --with-http_dav_module --with-http_flv_module --with-http_geoip_module \ --with-http_gzip_static_module --with-http_image_filter_module --with-http_perl_module \ --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module \ --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \ --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl \ --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module \ --add-module=../ngx_devel_kit \ --add-module=../echo-nginx-module \ --add-module=../lua-nginx-module \ --add-module=../redis2-nginx-module \ --add-module=../ngx_http_upstream_keepalive \ --add-module=../set-misc-nginx-module # make # make install 安装lua-redis-parser # git clone https://github.com/agentzh/lua-redis-parser.git # export LUA_INCLUDE_DIR=/usr/include/lua5.1 # make CC=gcc # make install CC=gcc 安装json # wget http://files.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip # unzip json4lua-0.9.50.zip # cp json4lua-0.9.50/json/json.lua /usr/share/lua/5.1/ 安装redis-lua # git clone https://github.com/nrk/redis-lua.git # cp redis-lua/src/redis.lua /usr/share/lua/5.1/
nginx配置
user www-data; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; error_log logs/error.log notice; pid logs/nginx.pid; worker_rlimit_nofile 60000; events { worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; access_log logs/access.log; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 60; types_hash_max_size 2048; server_tokens off; lua_code_cache on; upstream redis_pool { server 192.168.1.39:6379; keepalive 1024 single; //定义连接池大小,当连接数达到此数后,后续的连接为短连接 } server { listen 80; server_name 192.168.1.211; location /get_redis{ #internal; set_unescape_uri $key $arg_key; redis2_query hgetall $key; redis2_pass redis_pool; } location /json { content_by_lua_file conf/wcd.lua; } } }
wcd.lua
local json = require("json") local parser = require("redis.parser") local res = ngx.location.capture("/get_redis",{ args = { key = ngx.var.arg_key } }) if res.status == 200 then reply = parser.parse_reply(res.body) value = json.encode(reply) ngx.say(value) a = json.decode(value) ngx.say(a[2]) end
我的测试
server { listen 8888; server_name localhost; root html; index index.html index.htm; location /get { set_unescape_uri $key $arg_key; redis2_query get $key; redis2_pass 127.0.0.1:6379; } location /get_redis { #internal; set_unescape_uri $key $arg_key; redis2_query get $key; redis2_pass 127.0.0.1:6379; } location /json { set_unescape_uri $keys $arg_keys; content_by_lua_file html/redis2.lua; } }