#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { upstream master { server 10.1.1.100:9200; server 10.1.1.100:9200 backup;#备份库 keepalive 15; } upstream slave {#备份库 server 10.1.1.100:9200; keepalive 15; } server { server_name localhost 127.0.0.1 10.1.1.225; listen 8888; location / { content_by_lua_block { local function getFile(file_name) local f = assert(io.open(file_name, ‘r‘)) local string = f:read("*all") f:close() return string end ngx.req.read_body() ngx.log(ngx.ERR, ‘$$$‘ ..ngx.var.request_uri .. ‘$$$‘) local request_body = ngx.req.get_body_data() if nil == request_body then local file_name = ngx.req.get_body_file() if file_name then request_body = getFile(file_name) end end local master_resp, slave_resp local req_method_constant = ngx[‘HTTP_‘ .. ngx.req.get_method()] local arry = {method = req_method_constant, body = request_body} if req_method_constant == ngx.HTTP_POST or req_method_constant == ngx.HTTP_PUT or req_method_constant == ngx.HTTP_DELETE then master_resp, slave_resp = ngx.location.capture_multi{ {‘/slave‘ .. ngx.var.request_uri, arry}, {‘/master‘ .. ngx.var.request_uri, arry} } else master_resp = ngx.location.capture_multi{ {‘/master‘ .. ngx.var.request_uri, arry} } end --[[ 使用master的响应头 --]] for k,v in pairs(master_resp.header) do ngx.header[k] = v end ngx.say(master_resp.body) } } location ~* ^/master { internal; rewrite ^/master(.*)$ $1 break; proxy_pass http://master; } location ~* ^/slave { internal; log_subrequest on; rewrite ^/slave(.*)$ $1 break; proxy_pass http://slave; access_log logs/upstream.log; } } }
时间: 2024-10-06 06:20:39