Install memcached
yum install -y memcached
#启动memcached服务,端口11211,以nobody身份启动,-d后台模式运行
memcached -p11211 -u nobody -d
#将自己的IP设为灰度测试的IP
telnet 127.0.0.1 11211
set 119.32.216.122 0 0 1
1
quit
Install memcached for lua
wget https://github.com/openresty/lua-resty-memcached/archive/v0.11.tar.gz
tar -zxvf v0.11.tar.gz
mkdir -p /usr/local/share/lua/5.1
cp -r lua-resty-memcached-0.11/lib/resty /usr/local/share/lua/5.1
Create a file test.lua
clientIP=ngx.req.get_headers()["X-Real-IP"]
if clientIP == nil then
clientIP=ngx.req.get_headers()["x_forwarded_for"]
end
if clientIP == nil then
clientIP=ngx.var.remote_addr
end
local memcached=require "resty.memcached"
local memc,err=memcached:new()
if not memc then
ngx.say("failed to instantiate memc:",err)
return
end
local ok,err =memc:connect("127.0.0.1",11211)
if not ok then
ngx.say("failed to connect:",err)
return
end
local res,flags,err =memc:get(clientIP)
--ngx.say("value key: ",res,clientIP)
if err then
ngx.say("failed to get clientIP",err)
return
end
if res == "1" then
ngx.exec("@server_test")
return
end
ngx.exec("@server_product")
nginx configuration
server {
listen 8867;
server_name www.siguoya.name;
root /usr/local/nginx/1.12.1/html;
location / {
content_by_lua_file /etc/nginx/test.lua;
}
location /hello {
default_type text/plain;
content_by_lua 'ngx.say("hello lua")';
}
location /ua {
default_type text/plain;
content_by_lua '
clientIP=ngx.req.get_headers()["USER-AGENT"]
ngx.say("IP:",clientIP)
';
}
location @server_product {
default_type text/plain;
return 200 'product';
}
location @server_test {
default_type text/plain;
return 200 'test';
}
}
Since we will 119.32.216.122
this added to the IP memcached
, so the access http://www.siguoya.name:8867/
time to see the content that is test
after and open global agency, found that what you see is product
, so to achieve the effect of the gradation test for some users
Read topics
- 1. Nginx advantage
- 2. Nginx installation and boot from Kai
- 3. Nginx configuration syntax and directories
- 4. Nginx module
- 5. Nginx handling the static resources
- 6. Nginx browser cache principle
- 7. Cross-domain access resources Nginx
- Anti-theft chain 8. Nginx resources
- 9. Nginx proxy
- 10. Nginx Load Balancing
- 11. Nginx cache
- 12. Nginx static and dynamic separation
- 13. Nginx Rewrite
- 14. Nginx Secure Link
- 15. Nginx Geo
- 16. Nginx HTTPS service
- 17. Nginx and development of Lua
- 18. Nginx release and gray Lua
- 19. Nginx common errors
- 20. Nginx performance optimization
- 21. Nginx Security Management