Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Clean up nginx.conf. Move more logics from config file to uls.lua.
Browse files Browse the repository at this point in the history
Change-Id: Ia97b4a468c03c44e4b1a22c8502a7cbceedbd307
  • Loading branch information
anferneeg committed Feb 23, 2012
1 parent 9df59fd commit 6fad9ec
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 84 deletions.
99 changes: 15 additions & 84 deletions dev_setup/cookbooks/nginx/templates/default/ubuntu-nginx.conf.erb
Expand Up @@ -85,20 +85,15 @@ http {
-- add package.path and package.cpath
package.path = package.path..";<%= node[:lua][:module_path] %>/?.lua"
package.cpath = package.cpath..";<%= node[:lua][:module_path] %>/?.so"

local uls = require ("uls")
local cjson = require("cjson")

local uls_req_spec = {}
local req_stats = uls.serialize_request_statistics()
if req_stats then
uls_req_spec[uls.ULS_STATS_UPDATE] = req_stats
ngx.log(ngx.DEBUG, "monitor trigger stats syncup")
end
ngx.log(ngx.DEBUG, "monitor trigger stats syncup")

local req = uls.generate_stats_request()

-- generate one subrequest to uls with uls_req_spec
-- generate one subrequest to uls to update stats
ngx.location.capture(
"/vcapuls", {body = cjson.encode(uls_req_spec)}
"/vcapuls", { body = req }
)
';

Expand All @@ -112,10 +107,10 @@ http {
# ("if" block effectively creates a nested location and will inherit
# all the rewrite/access phase handlers of outer location)
if ($health_monitor != TT) {
# Backend server address returned from uls for this request
set $backend_addr '';
# Request tags returned from uls for this request to catalog statistics
set $uls_req_tags '';
# The following variables are used by lua module code.
# DO NOT remove or rename any of them!
set $backend_addr ''; # Backend server address returned from uls for this request
set $uls_req_tags ''; # Request tags returned from uls for this request to catalog statistics
set $router_ip '';
set $timestamp 0;
set $trace '';
Expand All @@ -125,51 +120,18 @@ http {
-- add package.path and package.cpath
package.path = package.path..";<%= node[:lua][:module_path] %>/?.lua"
package.cpath = package.cpath..";<%= node[:lua][:module_path] %>/?.so"

local uls = require ("uls")
local cjson = require("cjson")

if string.len(ngx.var.http_host) == 0 then
ngx.exit(ngx.HTTP_BAD_REQUEST)
end

ngx.var.timestamp = ngx.time()
uls.pre_process_subrequest(ngx)

local uls_req_spec = {}
uls_req_spec[uls.ULS_HOST_QUERY] = ngx.var.http_host
local req = uls.generate_uls_request(ngx)

local uls_sticky_session = uls.retrieve_vcap_sticky_session(
ngx.req.get_headers()[uls.COOKIE_HEADER])
if uls_sticky_session then
uls_req_spec[uls.ULS_STICKY_SESSION] = uls_sticky_session
ngx.log(ngx.DEBUG, "req sticks to backend session:"..uls_sticky_session)
end

if ngx.req.get_headers()[uls.VCAP_TRACE_HEADER] then
ngx.var.trace = "Y"
end

local req_stats = uls.serialize_request_statistics()
if req_stats then
uls_req_spec[uls.ULS_STATS_UPDATE] = req_stats
end

-- generate one subrequest to uls with uls_req_spec
-- generate one subrequest to uls for querying
local res = ngx.location.capture(
"/vcapuls", {body = cjson.encode(uls_req_spec)}
"/vcapuls", { body = req }
)

if res.status ~= 200 then
ngx.exit(ngx.HTTP_NOT_FOUND)
end

local msg = cjson.decode(res.body)
ngx.var.backend_addr = msg[uls.ULS_BACKEND_ADDR]
ngx.var.uls_req_tags = msg[uls.ULS_REQEST_TAGS]
ngx.var.router_ip = msg[uls.ULS_ROUTER_IP]
ngx.var.sticky = msg[uls.ULS_STICKY_SESSION]
ngx.log(ngx.DEBUG, "route request with host header:"..
ngx.var.http_host.." to:"..ngx.var.backend_addr)
uls.post_process_subrequest(ngx, res)
';

proxy_pass http://$backend_addr;
Expand All @@ -179,40 +141,9 @@ http {
-- add package.path and package.cpath
package.path = package.path..";<%= node[:lua][:module_path] %>/?.lua"
package.cpath = package.cpath..";<%= node[:lua][:module_path] %>/?.so"

local uls = require ("uls")

uls.vcap_store_stats(ngx.var.uls_req_tags, ngx.status,
(ngx.time() - ngx.var.timestamp) * 1000)

if ngx.var.trace == "Y" then
ngx.header[uls.VCAP_BACKEND_HEADER] = ngx.var.backend_addr
ngx.header[uls.VCAP_ROUTER_HEADER] = ngx.var.router_ip
end

local cookies = ngx.header.set_cookie
if not cookies then return end

if type(cookies) ~= "table" then cookies = {cookies} end
local sticky = false
for _, val in ipairs(cookies) do
local i, j = string.find(val:upper(), uls.STICKY_SESSIONS)
if i then
sticky = true
break
end
end
if not sticky then return end

local vcap_cookie = uls.VCAP_SESSION_ID.."="..ngx.var.sticky

ngx.log(ngx.DEBUG, "generate cookie:"..vcap_cookie.." for resp from:"..
ngx.var.backend_addr)
table.insert(cookies, vcap_cookie)
-- ngx.header.set_cookie incorrectly makes header to "set-cookie",
-- so workaround to set "Set-Cookie" directly
-- ngx.header.set_cookie = cookies
ngx.header["Set-Cookie"] = cookies
uls.post_process_response(ngx)
';
}
}
Expand Down
101 changes: 101 additions & 0 deletions router/ext/nginx/uls.lua
Expand Up @@ -150,3 +150,104 @@ function serialize_request_statistics()
stats_not_synced = {}
return stats
end
function vcap_handle_cookies(ngx)
local cookies = ngx.header.set_cookie
if not cookies then return end
if type(cookies) ~= "table" then cookies = {cookies} end
local sticky = false
for _, val in ipairs(cookies) do
local i, j = string.find(val:upper(), STICKY_SESSIONS)
if i then
sticky = true
break
end
end
if not sticky then return end
local vcap_cookie = VCAP_SESSION_ID.."="..ngx.var.sticky
ngx.log(ngx.DEBUG, "generate cookie:"..vcap_cookie.." for resp from:"..
ngx.var.backend_addr)
table.insert(cookies, vcap_cookie)
-- ngx.header.set_cookie incorrectly makes header to "set-cookie",
-- so workaround to set "Set-Cookie" directly
-- ngx.header.set_cookie = cookies
ngx.header["Set-Cookie"] = cookies
end
function vcap_add_trace_header(backend_addr, router_ip)
ngx.header[VCAP_BACKEND_HEADER] = backend_addr
ngx.header[VCAP_ROUTER_HEADER] = router_ip
end
function generate_stats_request()
local uls_req_spec = {}
local req_stats = uls.serialize_request_statistics()
if req_stats then
uls_req_spec[ULS_STATS_UPDATE] = req_stats
end
return cjson.encode(uls_req_spec)
end
function pre_process_subrequest(ngx)
ngx.var.timestamp = ngx.time()
if string.len(ngx.var.http_host) == 0 then
ngx.exit(ngx.HTTP_BAD_REQUEST)
end
if ngx.req.get_headers()[VCAP_TRACE_HEADER] then
ngx.var.trace = "Y"
end
end
function generate_uls_request(ngx)
local uls_req_spec = {}
-- add host in request
uls_req_spec[uls.ULS_HOST_QUERY] = ngx.var.http_host
-- add sticky session in request
local uls_sticky_session = retrieve_vcap_sticky_session(
ngx.req.get_headers()[COOKIE_HEADER])
if uls_sticky_session then
uls_req_spec[ULS_STICKY_SESSION] = uls_sticky_session
ngx.log(ngx.DEBUG, "req sticks to backend session:"..uls_sticky_session)
end
-- add status update in request
local req_stats = uls.serialize_request_statistics()
if req_stats then
uls_req_spec[ULS_STATS_UPDATE] = req_stats
end
return cjson.encode(uls_req_spec)
end
function post_process_subrequest(ngx, res)
if res.status ~= 200 then
ngx.exit(ngx.HTTP_NOT_FOUND)
end
local msg = cjson.decode(res.body)
ngx.var.backend_addr = msg[ULS_BACKEND_ADDR]
ngx.var.uls_req_tags = msg[ULS_REQEST_TAGS]
ngx.var.router_ip = msg[ULS_ROUTER_IP]
ngx.var.sticky = msg[ULS_STICKY_SESSION]
ngx.log(ngx.DEBUG, "route "..ngx.var.http_host.." to "..ngx.var.backend_addr)
end
function post_process_response(ngx)
local latency = ( ngx.time() - ngx.var.timestamp ) * 1000
vcap_store_stats(ngx.var.uls_req_tags, ngx.status, latency)
if ngx.var.trace == "Y" then
vcap_add_trace_header(ngx.var.backend_addr, ngx.var.router_ip)
end
vcap_handle_cookies(ngx)
end

0 comments on commit 6fad9ec

Please sign in to comment.