Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/etc/openresty_config/lua/
Upload File :
Current File : //opt/nginxhttpd_/etc/openresty_config/lua/forwarder_vhost.lua

--[[
The backup Vhost is called by Nginx when the cache server does not respond.
--]]

local o2utils = require "lib/o2switch_utils"
local o2redis = require "lib/o2switch_redis"
local o2debug = require "lib/o2switch_debug"
local o2config = require "lib/o2switch_config"
local cacheWrapper = require "lib/o2switch_cache_wrapper"
local e = o2redis.enum;
local type = type
local ngx = ngx

-- 1. Redis Key, remove the www + proxy subdomain if it's present
local name = o2utils.extractDomainWww(ngx.var.host)

if not name then
    --o2debug.debug("Direct access. No hostname provided.")
    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 err ~= nil or type(data[e.listenToIp]) ~= 'string') 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 err ~= nil or type(data[e.listenToIp]) ~= 'string' 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 err ~= nil or type(data[e.listenToIp]) ~= 'string' 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'))
        local err = 'Pas de configuration pour le domaine ' .. name .. ' (ipxtender/lscache/xtremcache actif/configuré ?)' -- TODO : In the future, create a nice page
        o2utils.ngxFinalError(503, err)
    end
end

-- Set Nginx's variables
if ngx.var.https == 'on' or ngx.req.get_headers()["x-forwarded-proto"] == 'https'
        or (type(ngx.req.get_headers()["x-forwarded-proto"]) == 'table' and ngx.req.get_headers()["x-forwarded-proto"][1] == 'https' ) then
    ngx.var.proxyPassProtocol = 'https'
    ngx.var.proxyPassIp = type(data[e.originalBackendIp]) == 'string' and data[e.originalBackendIp] or data[e.proxyPassSslIp]
    ngx.var.proxyPassPort = '4430'
    ngx.var.proxyPassFullLine =  'https://' .. (type(data[e.originalBackendIp]) == 'string' and data[e.originalBackendIp] or data[e.proxyPassSslIp]) .. ':4430'
else
    if data[e.proxyPassProtocol] == nil or data[e.proxyPassIp] == nil or data[e.proxyPassPort] == nil
            or data[e.proxyPassProtocol] == ngx.null or data[e.proxyPassIp] == ngx.null or data[e.proxyPassPort] == ngx.null 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.var.proxyPassProtocol = 'http'
    ngx.var.proxyPassIp = type(data[e.originalBackendIp]) == 'string' and data[e.originalBackendIp] or data[e.proxyPassSslIp]
    ngx.var.proxyPassPort = '8081'
    ngx.var.proxyPassFullLine = 'http://' .. (type(data[e.originalBackendIp]) == 'string' and data[e.originalBackendIp] or data[e.proxyPassSslIp]) .. ':8081'
end

--o2debug.debug(name .. " : " .. ngx.var.proxyPassFullLine)

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists