WordPress.org

Ready to get started?Download WordPress

Forums

Here's my lighttpd (or lighty) configuration for W3 Total Cache (5 posts)

  1. hozgur
    Member
    Posted 4 years ago #

    Hi,
    I just write a lua script which makes w3tc work in WordPress. I share it here, it may be useful for someone using lighttpd. If anyone finds a mistake please post here.

    Note that i only use w3tc for caching and minifying with default settings so i don't know if it is good for other settings.

    First, 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)" {
     magnet.attract-physical-path-to = ( "/absolute/path/w3tc.lua" )
    }

    Second, the lua script;

    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(wh)
    	lighty.header["X-Powered-By"] = "W3 Total Cache/0.8.5.2"
    	lighty.header["Pragma"] = "public"
    	if(wh == 1) then
    		lighty.header["Vary"] = "Accept-Encoding, Cookie"
    		lighty.header["X-Pingback"] = lighty.env["uri.scheme"] .. "://" .. lighty.env["uri.authority"] .. "/xmlrpc.php"
    	else
    		lighty.header["Vary"] = "Accept-Encoding"
    	end
    	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,wh)
    	if (lighty.stat(cached_page)) then
    		if(wh == 1) then
    			serve_headers(1)
    		else
    			serve_headers(2)
    		end
    		lighty.env["physical.path"] = cached_page
    		return true
    	else
    		return false
    	end
    end
    
    function serve_gzip(cached_page,wh)
    	if (lighty.stat(cached_page .. ".gzip")) then
    		lighty.header["Content-Encoding"] = "gzip"
    		if(wh == 1) then
    			lighty.header["Content-Type"] = "text/html"
    			serve_headers(1)
    		else
    			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(2)
    		end
    		lighty.env["physical.path"] = cached_page .. ".gzip"
    		return true
    	else
    		return false
    	end
    end
    
    if (string.match(lighty.env["physical.rel-path"], "/wp%-content/w3tc/min/")) then
    	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"]
    		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,2) then serve_html(cached_page,2) end
    		else
    			serve_html(cached_page,2)
    		end
    	end
    else
    	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")
    
    		if (query_condition and cookie_condition and method_condition) 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,1) then serve_html(cached_page,1) end
    			else
    				serve_html(cached_page,1)
    			end
    		end
    	end
    end
  2. hozgur
    Member
    Posted 4 years ago #

    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
  3. Frederick Townes
    Member
    Posted 4 years ago #

    Thanks for the contribution, I'll check it out soon.

  4. somee
    Member
    Posted 3 years ago #

    Hi. I tried to use these scripts for my site. I use "Mystique" theme and it works incorrect with these scripts. Main page looks good, but another pages don't.
    Screenshots are here: correct, incorrect
    I tried to solve this problem by myself, but i can't =( Any ideas? Thank you.

  5. Frederick Townes
    Member
    Posted 3 years ago #

    W3TC has changed quite a bit over the past 9 months. I don't think this configuration is at all viable anymore.

Topic Closed

This topic has been closed to new replies.

About this Topic