Sindbad~EG File Manager
--[[
This file will get the data for $domain from the redis storage.
It will fill up Nginx variables that are used in the proxy_pass part of Nginx.
It's a way to have fully dynamic Vhosts, backed with a Redis store and avoid creating Nginx vhosts files.
--]]
local o2utils = require "lib/o2switch_utils"
local o2redis = require "lib/o2switch_redis"
local o2config = require "lib/o2switch_config"
local o2debug = require "lib/o2switch_debug"
local cacheWrapper = require "lib/o2switch_cache_wrapper"
local type = type
local ngx = ngx
local e = o2redis.enum
--[[
Main code / logic
1. Extract the domain name, remove the WWW if present
2. Get value from cache or redis, populate cache is needed
3. Check that the request is on the right/expected IP
4. Set Nginx variables for the proxy pass
--]]
-- 1. Redis Key, remove the www + proxy subdomain if it's present
local name = o2utils.extractDomainWww(ngx.var.host)
if not name then
-- Should not happen, when no domain is provided the 'name' is equal to the IP address of the server
o2utils.ngxFinalError(503, 'Direct access denied, no hostname provided')
end
-- 2. Get the value from the cache, or from redis if we don't have it in the Nginx cache (populate the cache too)
local data, err = cacheWrapper.get(name, o2redis.getFromRedis, name)
if (type(data) ~= 'table' or not data[e.listenToIp] or data[e.listenToIp] == ngx.null)
and o2config.serverType == 'mutu' then
-- Remove common cPanel subdomain
name = o2utils.extractDomain(name)
data, err = cacheWrapper.get(name, o2redis.getFromRedis, name)
end
if type(data) ~= 'table' or not data[e.listenToIp] or data[e.listenToIp] == ngx.null then
-- If it's a mutu, we'll check for a wildcard and default to the defautProxyPass ...
if o2config.serverType == 'mutu' then
-- Can be a wildcard case, try again but we transform the first subdom to wildcard this time
name = o2utils.transformSubdomainToWildcard(name)
data, err = cacheWrapper.get(name, o2redis.getFromRedis, name)
-- Still not result, we use the default proxypass ...
if type(data) ~= 'table' or not data[e.listenToIp] or data[e.listenToIp] == ngx.null then
--o2debug.debug('default proxy pass info returned');
o2utils.defaultProxyPassMutu()
return
end
else
-- On a edge server, we return an error if nothing is found in the first place
--o2debug.debug("No vhost found for : " .. name .. ". Err = " .. (err or 'no err msg'))
err = 'Pas de configuration pour le domaine ' .. name .. ' (ipxtender/lscache/xtremcache actif/configuré ?)'
o2utils.ngxFinalError(503, err)
end
end
-- 3. Make sure the access is on the right IP
if not data[e.listenToIp] or data[e.listenToIp] ~= ngx.var.server_addr then
-- Wrong IP on mutu = default page for cPanel
-- We could return a 502 too if we want
if o2config.serverType == 'mutu' then
o2utils.defaultProxyPassMutu()
return
else
-- ngx.log(ngx.ERR, "Access to vhost on wrong IP " .. name)
-- local okip = data[e.listenToIp] or 'err'
-- local err = 'Une configuration serveur existe pour ' .. name .. ' mais le domaine pointe sur la mauvaise adresse IP : ' .. ngx.var.server_addr .. ' a la place de ' .. okip
-- o2utils.ngxFinalError(503, err)
end
end
-- 4. Set Nginx's variables
if ngx.var.https == 'on' then
if type(data[e.proxyPassSslProtocol]) ~= 'string' or type(data[e.proxyPassSslIp]) ~= 'string' or type(data[e.proxyPassSslPort]) ~= 'string' then
--o2debug.debug("No vhost (https) data for : " .. name)
local err = 'Une configuration serveur semble exister pour ' .. name .. ' mais pas pour la version HTTPS'
o2utils.ngxFinalError(503, err)
end
ngx.ctx.proxyPassProtocol = data[e.proxyPassSslProtocol]
ngx.ctx.proxyPassIp = data[e.proxyPassSslIp]
ngx.ctx.proxyPassPort = data[e.proxyPassSslPort]
ngx.var.proxyPassFullLine = data[e.proxyPassSslProtocol] .. '://' .. data[e.proxyPassSslIp] .. ':' .. data[e.proxyPassSslPort]
else
if type(data[e.proxyPassProtocol]) ~= 'string' or type(data[e.proxyPassIp]) ~= 'string' or type(data[e.proxyPassPort]) ~= 'string' then
--o2debug.debug("No vhost (http) data for : " .. name)
local err = 'Pas de configuration pour le domaine ' .. name .. ' (ipxtender/lscache/xtremcache actif/configuré ?)'
o2utils.ngxFinalError(503, err)
end
ngx.ctx.proxyPassProtocol = data[e.proxyPassProtocol]
ngx.ctx.proxyPassIp = data[e.proxyPassIp]
ngx.ctx.proxyPassPort = data[e.proxyPassPort]
ngx.var.proxyPassFullLine = data[e.proxyPassProtocol] .. '://' .. data[e.proxyPassIp] .. ':' .. data[e.proxyPassPort]
end
-- 5. Special case if backend if a cache server, send to varnish_backend or lslb_backend.
if ngx.ctx.proxyPassIp == o2config.varnish_ip and ngx.ctx.proxyPassPort == o2config.varnish_port then
-- if ngx.var.request_method == 'PURGE' then
-- --o2debug.debug("On varnish + Method Purge detected, override to https://chained_purge")
-- ngx.var.proxyPassFullLine = 'https://chained_purge'
-- else
--o2debug.debug("Varnish detected, override to ://varnish_backend")
ngx.var.proxyPassFullLine = ngx.ctx.proxyPassProtocol .. '://varnish_backend'
-- end
elseif ngx.ctx.proxyPassIp == o2config.lslb_ip and ngx.ctx.proxyPassPort == o2config.lslb_port then
--o2debug.debug("Litespeed detected, override to ://lslb_backend")
ngx.var.proxyPassFullLine = ngx.ctx.proxyPassProtocol .. '://lslb_backend'
end
--o2debug.debug("Full proxy pass line is : " .. ngx.var.proxyPassFullLine)
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists