在CentOS 6.9 x86_64的OpenResty 1.13.6.1上使用基于Redis实现动态路由示例
生活随笔
收集整理的這篇文章主要介紹了
在CentOS 6.9 x86_64的OpenResty 1.13.6.1上使用基于Redis实现动态路由示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下面是我閱讀春哥OpenResty官網主頁中“Dynamic Routing Based On Redis”一節的實操記錄,整理如下。
https://openresty.org/cn/dynamic-routing-based-on-redis.html
這個例子展示了利用Redis將進來的請求,依據User-Agent頭的不同,路由到不同的后端HTTP服務器上面。
這個demo將會使用到OpenResty打包的Redis2 Nginx Module, Lua Nginx Module, Lua Redis Parser Library, 和Set Misc Nginx Module等模塊
1.安裝redis
2.配置OpenResty
下面是nginx.conf的配置
3.開啟redis并灌入一些測試數據
在localhost:6369上開啟redis服務器
./redis-server
使用redis-cli工具填入一些數據
./redis-cli
? ?redis> set foo apache.org
? ?OK
? ?redis> set bar nginx.org
? ?OK
4.開啟終端進行測試
curl -vo /tmp/apache.org --user-agent foo localhost:8080
? ?
curl -vo /tmp/nginx.org --user-agent bar localhost:8080 5.進一步的性能改進
https://openresty.org/cn/dynamic-routing-based-on-redis.html
這個例子展示了利用Redis將進來的請求,依據User-Agent頭的不同,路由到不同的后端HTTP服務器上面。
這個demo將會使用到OpenResty打包的Redis2 Nginx Module, Lua Nginx Module, Lua Redis Parser Library, 和Set Misc Nginx Module等模塊
1.安裝redis
參見本博博文
http://blog.csdn.net/tao_627/article/details/78936447
2.配置OpenResty
下面是nginx.conf的配置
worker_processes 1;
user root;
error_log logs/error.log info;events {worker_connections 1024;
}http {upstream apache.org {server apache.org;}upstream nginx.org {server nginx.org;}server {listen 8080;location = /redis {internal;set_unescape_uri $key $arg_key;redis2_query get $key;redis2_pass 127.0.0.1:6379;}location / {set $target '';access_by_lua 'local key = ngx.var.http_user_agentlocal res = ngx.location.capture("/redis", { args = { key = key } })print("key: ", key)if res.status ~= 200 thenngx.log(ngx.ERR, "redis server returned bad status: ",res.status)ngx.exit(res.status)endif not res.body thenngx.log(ngx.ERR, "redis returned empty body")ngx.exit(500)endlocal parser = require "redis.parser"local server, typ = parser.parse_reply(res.body)if typ ~= parser.BULK_REPLY or not server thenngx.log(ngx.ERR, "bad redis response: ", res.body)ngx.exit(500)endprint("server: ", server)ngx.var.target = server';proxy_pass http://$target;}}
}
3.開啟redis并灌入一些測試數據
在localhost:6369上開啟redis服務器
./redis-server
使用redis-cli工具填入一些數據
./redis-cli
? ?redis> set foo apache.org
? ?OK
? ?redis> set bar nginx.org
? ?OK
4.開啟終端進行測試
curl -vo /tmp/apache.org --user-agent foo localhost:8080
? ?
curl -vo /tmp/nginx.org --user-agent bar localhost:8080 5.進一步的性能改進
對性能調優,我們能想到的就是,對redis連接開啟連接池,參見Redis2 Nginx Module's README等文檔
6.參考文獻
[1].https://openresty.org/cn/dynamic-routing-based-on-redis.html
總結
以上是生活随笔為你收集整理的在CentOS 6.9 x86_64的OpenResty 1.13.6.1上使用基于Redis实现动态路由示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在CentOS 6.9 x86_64的O
- 下一篇: 在CentOS 6.9 x86_64上安