#user nobody;
worker_processes
1;
#pid logs/nginx.pid;
events {
worker_connections
1024;
}
http {
include mime.types;
default_type application
/octet-stream;
sendfile on;
keepalive_timeout
65;
init_by_lua
'
redis = require "resty.redis" numbers
= {["0"]="10.0.0.148",["1"]="10.0.0.248"}';
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /echo {
default_type text/plain;
echo hello lua;
}
location /test {
default_type text_plain;
content_by_lua 'ngx.say("Hi lile")';
}
location /re {
default_type text_plain;
content_by_lua '
local headers = ngx.req.get_headers()
local ip = headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0"
ngx.say(ip.."#"..os.time().."000")
';
}
location /test2 {
#default_type application/json;
default_type text_plain;
content_by_lua '
local redis_client = redis.new()
local arg = ngx.var.arg_uuid
local uuid_f = string.sub(arg,0,1)
local ok,err=redis_client.connect(redis_client,numbers[uuid_f],"6379")
if not ok then
ngx.say("failed to connect:",err)
return
end
local res = redis_client:get("foo")
if res==ngx.null then
ngx.say("This is null")
else
ngx.say(res)
end
redis_client:close()
';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
总结:
从昨天开始弄这个的时候,完全没接触过啊,redis 是数据库都不知道,只知道这个名字,lua也是第一次听啊,nginx一直想好好研究一下,但是都没行动过,然后就使劲的在网上找资料,发现基本上都一样,弄的很复杂很复杂,今天早上来,不知道为啥,突然就大脑很清醒,想的很简单,没想到反而这样可以,可能是昨晚睡得比较好,精神也好,最近被达康书记给迷倒了,每天熬夜追剧,话说真的睡眠真的可以让你精神倍儿好,心情也会好。nginx通过lua,根据自己的规则去访问redis服务器,首先我肯定得用lua实现nginx与redis的连接,所以就有那个加载模块,创建对象,连接这几个步骤,这相当于只有你想连上redis,你就得这么做,然后我要根据参数来定规则,我肯定得获取这个参数的值,然后进行转换....当然,这只是事后的想法,现在都不相信我居然弄了这个,虽然过两天,这些又忘了,但是至少我知道有这个东西,知道怎么分析问题,而不是一个劲的去搜怎么做。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com