使用Nginx Lua实现redis高性能http接口时间 2015-01-27 18:26:53 峰云就她了原文 http://xiaorui.cc/2015/01/27/使用nginx-lua实现redis高性能http接口/主题 Lua Nginx HTTP某些时候用python实现的接口性能不够强劲,需要换种方法来提高性能,这不就用lua试试。 常见的redis lua的组合功能 有接口防止过度访问,动态的数据加载,接口的数据缓存 redis没有直接提供一个http的接口, 性能的比较的话,lua的能力要比php、python要强的不少。。。网上有很多的性能的比较,我也做过几次的压力测试,lua的性能确实很强。。。。Http://xiaorui.ccHttp://xiaorui.ccHttp://xiaorui.cc废话,首先安装nginx,然后加入lua环境。 Pythongit clone https://github.com/simpl/ngx_devel_kit.gitgit clone https://github.com/chaoslawful/lua-nginx-module.gitgit clone https://github.com/agentzh/redis2-nginx-module.gitgit clone https://github.com/agentzh/set-misc-nginx-module.gitgit clone https://github.com/agentzh/echo-nginx-module.gityum -y install pcre pcre-dev*wget http://nginx.org/download/nginx-1.3.14.tar.gztar zxvf nginx-1.3.14.tar.gzcd nginx-1.3.14./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit/ --add-module=../lua-nginx-module --add-module=../redis2-nginx-module --add-module=../set-misc-nginx-module --add-module=../echo-nginx-modulemake && make install注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。lua_code_cache off;下面是 Nginx.conf 的配置 Pythonserver{ listen 80; server_name test.lua.com; #http://test.lua.com/lua location /hello { default_type "text/plain"; content_by_lua 'ngx.say("Nginx Lua Hello!")'; } #GET http://test.lua.com/get?key=key location /get { set_unescape_uri $key $arg_key; redis2_query get $key; redis2_pass 127.0.0.1:6379; #配置redis访问 } #SET http://test.lua.com/set?key=key&val=value location /set { set_unescape_uri $key $arg_key; set_unescape_uri $val $arg_val; redis2_query set $key $val; redis2_pass 127.0.0.1:6379; }}重启Nginx/etc/init.d/nginx restart也可以直接用lua调用redis的接口 Pythonlocal ckid = redis.pcall('get',KEYS[1])local metaif ckid ~= nil then meta = redis.call('hgetall', ckid)else meta = 'none' ckid = 'none'endreturn {ckid, meta}这里再分享一个比较完整的实例: Pythonlocation /foo { set $value 'first'; redis2_query set one $value; redis2_pass 127.0.0.1:6379; } # GET /get?key=some_key location /get { set_unescape_uri $key $arg_key; # this requires ngx_set_misc redis2_query get $key; redis2_pass foo.com:6379; } # GET /set?key=one&val=first%20value location /set { set_unescape_uri $key $arg_key; # this requires ngx_set_misc set_unescape_uri $val $arg_val; # this requires ngx_set_misc redis2_query set $key $val; redis2_pass foo.com:6379; } # multiple pipelined queries location /foo { set $value 'first'; redis2_query set one $value; redis2_query get one; redis2_query set one two; redis2_query get one; redis2_pass 127.0.0.1:6379; } location /bar { # $ is not special here... redis2_literal_raw_query '*1\r\n$4\r\nping\r\n'; redis2_pass 127.0.0.1:6379; } location /bar { # variables can be used below and $ is special redis2_raw_query 'get one\r\n'; redis2_pass 127.0.0.1:6379; } # GET /baz?get%20foo%0d%0a location /baz { set_unescape_uri $query $query_string; # this requires the ngx_set_misc module redis2_raw_query $query; redis2_pass 127.0.0.1:6379; } location /init { redis2_query del key1; redis2_query lpush key1 C; redis2_query lpush key1 B; redis2_query lpush key1 A; redis2_pass 127.0.0.1:6379; } location /get { redis2_query lrange key1 0 -1; redis2_pass 127.0.0.1:6379; }如果你的逻辑比较简单推荐用lua试试,如果逻辑比较的复杂,那我觉得还是慎重点的,不然花费学习lua的时间还不如用python的tornado flask这样的web框架开个多进程来做端口轮训。