Install memcached

Yum install -y memcached # Start memcached service on port 11211, start as nobody, -d background mode running memcached-p11211-u nobody -d # set your IP to grayscale test IP Telnet 127.0.0.1 11211 set 119.32.216.122 0 0 11 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 the test.lua file

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'; }}

Due to we add this 119.32.216.122 IP into memcached, so visit http://www.siguoya.name:8867/, see is the test and after open global agents, found that the content is the product of, In this way, we can achieve the effect of gray test for some users

Special reading

  • 1. Advantages of Nginx
  • 2. Nginx installation and bootstart
  • 3. Nginx directory and configuration syntax
  • 4. Nginx module
  • 5. Nginx static resource handling
  • 6. Principle of Nginx Browser Caching
  • 7. Cross-domain access to Nginx resources
  • 8. Hotlinking protection of NGINX resources
  • 9. Nginx proxy
  • 10. Nginx load balancing
  • 11. Nginx cache
  • 12. Nginx static and static separation
  • 13. Nginx Rewrite
  • 14. Nginx Secure Link
  • 15. Nginx Geo
  • 16. Nginx HTTPS service
  • 17. Nginx and Lua development
  • 18. Nginx and Lua grayscale publishing
  • 19. Common errors in Nginx
  • 20. Nginx performance optimization
  • 21. Nginx security management