1、系统基础设计图为:
用户通过Http访问Openresty(Nginx + Lua), 其中Nginx虚拟主机中配置文件进行Lua脚本加载。
LUA通过nginx内置变量或者http请求中变量来区分不同集群,使用LUA脚本从redis中获取所属集群。
然后通过反向代理到对应集群。
2、 Openresty配置
Nginx主配置文件:
user nginx;
worker_processes ;
pid logs/nginx.pid; worker_rlimit_nofile ; events {
use epoll;
worker_connections ;
} http {
lua_package_path "/usr/local/openresty/lualib/resty/lrucache/?.lua;;"; # 加载lua自定义模块。
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request $http_host '
'$status $body_bytes_sent $http_referer '
'"$http_user_agent" $http_x_forwarded_for '
'$upstream_addr $upstream_status $upstream_cache_status '
'$upstream_response_time $request_time';
access_log logs/access.log main;
error_log logs/error.log;
sendfile on;
tcp_nopush on;
max_ranges ;
keepalive_timeout ;
gzip on;
gzip_min_length 1k;
gzip_buffers 16k;
gzip_comp_level ;
gzip_types application/x-www-form-urlencoded application/pdf text/plain application/x-javascript application/javascript text/css application/xml application/json;
client_max_body_size 22m;
client_body_buffer_size 1024k;
client_header_buffer_size 1024k;
underscores_in_headers on; location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
include /data/work/conf/conf.d/*/*.conf;
虚拟主机的配置:
server {
listen ;
server_name 域名;
access_log logs/access_80.log main;
error_log logs/error_80.log; location / {
resolver DNS的IP地址 valid=; 如果配置文件中使用了域名,需要此配置,不然无法解析DNS。
set $url ''; 定义变量
set $upstream_port ''; 变量赋值
rewrite_by_lua_file lua/get_upstream_url.lua; 加载lua脚本
proxy_pass http://$url$request_uri;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header connection "keep-alive";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout ;
proxy_send_timeout ;
proxy_connect_timeout ;
proxy_temp_file_write_size 10240k;
}
}
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()
local ok, err = red:connect("redis地址", )
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(, )
if not ok then
ngx.log(ngx.ERR, "failed to set keepalive: ", err)
return
end
end
自定义本地缓存的lua模块脚本
local _M = {} local lrucache = require "resty.lrucache" local c, err = lrucache.new()
if not c then
return error("failed to create the cache: " .. (err or "unknown"))
end function _M.get(host)
local key = c:get(host)
return key
end function _M.set(host, key)
c:set(host, key, )
local res = c:get(host)
end return _M