Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter hozgur

    (@hozgur)

    Update: I split the lua code into 2 and also edit lighttpd.conf. It should be more efficient now.

    lighttpd.conf

    $HTTP["url"] =~ "\.(css|js)$" {
        expire.url = ( "" => "modification plus 24 hours" )
    }
    
    $HTTP["url"] =~ "\.(html|htm|stm)$" {
        expire.url = ( "" => "modification plus 60 minutes" )
    }
    
    $HTTP["host"] =~ "^(www.yourdomain.com|yourdomain.com)" {
    
    	$HTTP["url"] =~ "/wp\-content/w3tc/min/" {
    		magnet.attract-physical-path-to = ( "/etc/lighttpd/w3tmin.lua" )
    	}
    	$HTTP["url"] !~ "/(wp\-content|wp\-admin)/" {
    		magnet.attract-physical-path-to = ( "/etc/lighttpd/w3tcache.lua" )
    	}
    
    }

    w3tmin.lua

    function explode(d,p)
    	local t, ll
    	t={}
    	ll=0
    	if(#p == 1) then return p end
    		while true do
    			l=string.find(p,d,ll+1,true)
    			if l~=nil then
    				table.insert(t, string.sub(p,ll,l-1))
    				ll=l+1
    			else
    				table.insert(t, string.sub(p,ll))
    				break
    			end
    		end
    	return t
    end
    
    function serve_headers()
    	lighty.header["X-Powered-By"] = "W3 Total Cache/0.8.5.2"
    	lighty.header["Pragma"] = "public"
    	lighty.header["Vary"] = "Accept-Encoding"
    	cachecontrol = lighty.header["Cache-Control"] or ""
    	if(cachecontrol == "") then
    		lighty.header["Cache-Control"] = "public, must-revalidate, proxy-revalidate"
    	else
    		lighty.header["Cache-Control"] = cachecontrol .. ", public, must-revalidate, proxy-revalidate"
    	end
    end
    
    function serve_html(cached_page)
    	if (lighty.stat(cached_page)) then
    		serve_headers()
    		lighty.env["physical.path"] = cached_page
    		return true
    	else
    		return false
    	end
    end
    
    function serve_gzip(cached_page)
    	if (lighty.stat(cached_page .. ".gzip")) then
    		lighty.header["Content-Encoding"] = "gzip"
    		if(string.match(lighty.env["physical.rel-path"], "%.js$")) then
    			lighty.header["Content-Type"] = "application/x-javascript"
    		else
    			lighty.header["Content-Type"] = "text/css"
    		end
    		serve_headers()
    		lighty.env["physical.path"] = cached_page .. ".gzip"
    		return true
    	else
    		return false
    	end
    end
    
    attr = lighty.stat(lighty.env["physical.path"] .. ".gzip")
    if (not attr) then
    	attr = lighty.stat(lighty.env["physical.path"])
    	if (not attr) then
    		minfile =  string.gsub(lighty.env["uri.path"], "/wp%-content/w3tc/min/","")
    		gggt = explode(".",minfile)
    
    		lighty.env["uri.path"] = "/wp-content/w3tc/min/index.php"
    		lighty.env["uri.query"] = "gg=" .. gggt[1] .."&g=" .. gggt[2] .."&t=" .. gggt[3]
    		lighty.env["physical.rel-path"] = lighty.env["uri.path"]
    		lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
    	else
    		cached_page = lighty.env["physical.path"]
    		serve_html(cached_page)
    	end
    else
    	accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
    
    	cached_page = lighty.env["physical.path"]
    
    	if (string.find(accept_encoding, "gzip")) then
    		if not serve_gzip(cached_page) then serve_html(cached_page) end
    	else
    		serve_html(cached_page)
    	end
    end

    w3tcache.lua

    function serve_headers()
    	lighty.header["X-Powered-By"] = "W3 Total Cache/0.8.5.2"
    	lighty.header["Pragma"] = "public"
    	lighty.header["Vary"] = "Accept-Encoding, Cookie"
    	lighty.header["X-Pingback"] = lighty.env["uri.scheme"] .. "://" .. lighty.env["uri.authority"] .. "/xmlrpc.php"
    	cachecontrol = lighty.header["Cache-Control"] or ""
    	if(cachecontrol == "") then
    		lighty.header["Cache-Control"] = "public, must-revalidate, proxy-revalidate"
    	else
    		lighty.header["Cache-Control"] = cachecontrol .. ", public, must-revalidate, proxy-revalidate"
    	end
    end
    
    function serve_html(cached_page)
    	if (lighty.stat(cached_page)) then
    		serve_headers()
    		lighty.env["physical.path"] = cached_page
    		return true
    	else
    		return false
    	end
    end
    
    function serve_gzip(cached_page)
    	if (lighty.stat(cached_page .. ".gzip")) then
    		lighty.header["Content-Encoding"] = "gzip"
    		lighty.header["Content-Type"] = "text/html"
    		serve_headers()
    		lighty.env["physical.path"] = cached_page .. ".gzip"
    		return true
    	else
    		return false
    	end
    end
    
    attr = lighty.stat(lighty.env["physical.path"])
    if (not attr) then
    	lighty.env["uri.path"] = "/index.php"
    	lighty.env["physical.rel-path"] = lighty.env["uri.path"]
    	lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
    
    	user_query = lighty.env["uri.query"] or ""
    	query_condition = (user_query == "")
    	user_cookie = lighty.request["Cookie"] or ""
    	cookie_condition = not (string.find(user_cookie, ".*comment_author.*") or string.find(user_cookie, ".*wordpress.*") or string.find(user_cookie, ".*wp-postpass_.*"))
    	user_method = lighty.env["request.method"] or ""
    	method_condition = (user_method ~= "POST")
    
    	local userAgentsNoCaching = { "bot", "ia_archive", "slurp", "crawl", "spider", "iphone", "ipod", "android", "cupcake", "webos", "incognito", "webmate", "opera mini", "blackberry", "symbian", "series60", "nokia", "samsung" }
    	userAgent = lighty.request["User-Agent"]
    	if (nil == userAgent) then
    		sendCachedFile = true
    	else
    		userAgent = string.lower(userAgent)
    		for i, v in ipairs(userAgentsNoCaching) do
    			if string.find(v, userAgent) then
    				sendCachedFile = false
    				break
    			end
    		end
    	end
    
    	if (query_condition and cookie_condition and method_condition and sendCachedFile) then
    		accept_encoding = lighty.request["Accept-Encoding"] or "no_acceptance"
    		cached_page = lighty.env["physical.doc-root"] .. "/wp-content/w3tc/pgcache/" .. lighty.env["request.uri"] .. "/_default_.html"
    		cached_page = string.gsub(cached_page, "//", "/")
    
    		if (string.find(accept_encoding, "gzip")) then
    			if not serve_gzip(cached_page) then serve_html(cached_page) end
    		else
    			serve_html(cached_page)
    		end
    	end
    end

Viewing 1 replies (of 1 total)