http {
lua_package_path '/var/app/?.lua;;';
server {
set $auth_cookie "";
location @handle_401 {
rewrite_by_lua_block {
local auth = require("auth")
local is_auth = auth.auth()
if not is_auth then
return ngx.exit(ngx.HTTP_UNAUTHORIZED)
else
ngx.var.auth_cookie = auth.auth_cookie
end
}
proxy_set_header Cookie $auth_cookie;
more_clear_headers "Set-Cookie";
proxy_pass <THE RESOURCE>
}
location / {
access_by_lua_block {
local auth = require("auth")
local is_auth = auth.auth()
}
proxy_intercept_errors on;
error_page 401 = @handle_401;
proxy_set_header Cookie $auth_cookie;
more_clear_headers "Set-Cookie";
proxy_pass <THE RESOURCE>
}
}
}