1、系统基础设计图为:
用户通过Http访问Openresty(Nginx + Lua), 其中Nginx虚拟主机中配置文件进行Lua脚本加载。
LUA通过nginx内置变量或者http请求中变量来区分不同集群,使用LUA脚本从redis中获取所属集群。
然后通过反向代理到对应集群。
2、 Openresty配置
Nginx主配置文件:
1 user nginx; 2 worker_processes 4; 3 pid logs/nginx.pid; 4 5 worker_rlimit_nofile 1000000; 6 7 events { 8 use epoll; 9 worker_connections 65535; 10 } 11 12 http { 13 lua_package_path "/usr/local/openresty/lualib/resty/lrucache/?.lua;;"; # 加载lua自定义模块。 14 include mime.types; 15 default_type application/octet-stream; 16 log_format main '$remote_addr - $remote_user [$time_local] $request $http_host ' 17 '$status $body_bytes_sent $http_referer ' 18 '"$http_user_agent" $http_x_forwarded_for ' 19 '$upstream_addr $upstream_status $upstream_cache_status ' 20 '$upstream_response_time $request_time'; 21 access_log logs/access.log main; 22 error_log logs/error.log; 23 sendfile on; 24 tcp_nopush on; 25 max_ranges 1; 26 keepalive_timeout 120; 27 gzip on; 28 gzip_min_length 1k; 29 gzip_buffers 4 16k; 30 gzip_comp_level 2; 31 gzip_types application/x-www-form-urlencoded application/pdf text/plain application/x-javascript application/javascript text/css application/xml application/json; 32 client_max_body_size 22m; 33 client_body_buffer_size 1024k; 34 client_header_buffer_size 1024k; 35 underscores_in_headers on; 36 37 location /status { 38 stub_status on; 39 access_log off; 40 allow 127.0.0.1; 41 deny all; 42 } 43 } 44 include /data/work/conf/conf.d/*/*.conf;
虚拟主机的配置:
1 server { 2 listen 80; 3 server_name 域名; 4 access_log logs/access_80.log main; 5 error_log logs/error_80.log; 6 7 8 location / { 9 resolver DNS的IP地址 valid=3600; 如果配置文件中使用了域名,需要此配置,不然无法解析DNS。 10 set $url ''; 定义变量 11 set $upstream_port '80'; 变量赋值 12 rewrite_by_lua_file lua/get_upstream_url.lua; 加载lua脚本 13 proxy_pass http://$url$request_uri; 14 proxy_http_version 1.1; 15 proxy_set_header Upgrade $http_upgrade; 16 proxy_set_header connection "keep-alive"; 17 proxy_set_header Host $host; 18 proxy_set_header X-Forwarded-Port $server_port; 19 proxy_set_header X-Forwarded-For $remote_addr;21 proxy_set_header X-Real-IP $remote_addr; 22 proxy_read_timeout 300; 23 proxy_send_timeout 300; 24 proxy_connect_timeout 90; 25 proxy_temp_file_write_size 10240k; 26 } 27 }
lua 脚本
local localCache = require("mycache") 本地缓存配置 local localRes, err = localCache.get(ngx.var.host) if localRes ~= nil then ngx.var.url = localRes .. ":" .. ngx.var.upstream_port return else local redis = require "resty.redis" local red = redis:new() red:set_timeout(1000) local ok, err = red:connect("redis地址", 6379) if not ok then ngx.log(ngx.ERR, "failed to connect: ", err) return end local res, err = red:hget("hosts", ngx.var.host) if not res then ngx.log(ngx.ERR, "failed to get host: ", err) return end if res == ngx.null then ngx.log(ngx.ERR, "host not found.") ngx.var.url = "异常网址" return else localCache.set(ngx.var.host, res) ngx.var.url = res .. ":" .. ngx.var.upstream_port end local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.log(ngx.ERR, "failed to set keepalive: ", err) return end end
自定义本地缓存的lua模块脚本
1 local _M = {} 2 3 local lrucache = require "resty.lrucache" 4 5 local c, err = lrucache.new(10000) 6 if not c then 7 return error("failed to create the cache: " .. (err or "unknown")) 8 end 9 10 function _M.get(host) 11 local key = c:get(host) 12 return key 13 end 14 15 function _M.set(host, key) 16 c:set(host, key, 3600) 17 local res = c:get(host) 18 end 19 20 return _M