{"id":3517,"date":"2016-06-07T00:04:50","date_gmt":"2016-06-07T00:04:50","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=3517"},"modified":"2017-10-19T03:46:14","modified_gmt":"2017-10-19T03:46:14","slug":"sysadmin-serve-your-wordpress-cached-pages-with-lighttpd-and-not-php","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/sysadmin-serve-your-wordpress-cached-pages-with-lighttpd-and-not-php\/","title":{"rendered":"[SYSADMIN] Serve your WordPress cached pages directly with lighttpd and not PHP"},"content":{"rendered":"<h1>Optimizing Your WordPress Cache Loads in Lighttpd.<\/h1>\n<p>If you don&#8217;t configure your wordpress virtual host properly in lighttpd, your wordpress cache will still make use of PHP.<\/p>\n<p>Wouldn&#8217;t it be nice if all those cached requests were served directy from the webserver as the static files that they are, bypassing the CPU\/memory load PHP can have, and use those resources for otherthings?<\/p>\n<h1>Install and Enable mod_magnet<\/h1>\n<p>For this to occur with lighttpd, you will need mod_magnet, so assuming you&#8217;re on a Ubuntu\/Debian based linux distro let&#8217;s make sure we have it installed.<\/p>\n<p><code>sudo apt-get install lighttpd-mod-magnet<\/code><\/p>\n<p>Then let&#8217;s make sure it&#8217;s enabled, you can do this manually on your lighttpd.conf by adding &#8220;mod_magnet&#8221; to the list of enabled modules&#8230;<\/p>\n<pre><code>server.modules = (\n        \"mod_fastcgi\",\n        \"mod_access\",\n        \"mod_alias\",\n        \"mod_accesslog\",\n        \"mod_compress\",\n        \"mod_rewrite\",\n        \"mod_redirect\",\n        \"mod_status\",\n        \"mod_proxy\",\n        \"mod_setenv\",\n        \"mod_magnet\"\n)\n<\/code><\/pre>\n<p>or you can do it the lighty way:<\/p>\n<p><code>sudo lighty-enable-mod magnet<\/code><\/p>\n<p>(this simply makes a symlink to the <code>10-magnet.conf<\/code> file inside <code>\/etc\/lighttpd\/conf-enabled<\/code> which lighty will check upon startup)<\/p>\n<h1>The cache logic script that will be executed by lighttpd<\/h1>\n<p>Now, on your wordpress directory, create a file called <code>rewrite.lua<\/code> and paste the following script in it:<\/p>\n<pre><code>function log(str)\n   -- wanna tail -f a log to see what's happening    \n   fp = io.open(\"\/path\/to\/some\/lua.log\",\"a+\")\n   fp:write(str .. \"\\n\")\n   fp:flush()\n   fp:close()\nend\n\nfunction serve_html(cached_page)\n    if (lighty.stat(cached_page)) then\n        lighty.env[\"physical.path\"] = cached_page\n        return true\n    else\n        return false\n    end\nend\n\nfunction serve_gzip(cached_page)\n    if (lighty.stat(cached_page .. \".gz\")) then\n        lighty.header[\"Content-Encoding\"] = \"gzip\"\n        lighty.header[\"Content-Type\"] = \"\"\n        lighty.env[\"physical.path\"] = cached_page .. \".gz\"\n        return true\n    else\n        return false\n    end\nend\n\nif (lighty.env[\"uri.scheme\"] == \"http\") then\n    ext = \".html\"\nelse\n    ext = \"-https.html\"\nend\n\ncached_page = lighty.env[\"physical.doc-root\"] .. \"\/wp-content\/cache\/supercache\/\" .. lighty.request[\"Host\"] .. lighty.env[\"request.orig-uri\"]\ncached_page = string.gsub(cached_page, \"\/\/\", \"\/\")\ncached_page = string.gsub(cached_page, lighty.request[\"Host\"] .. \"\/index.php\", lighty.request[\"Host\"])\n\nattr = lighty.stat(cached_page)\n\nif (attr) then\n    query_condition = not (lighty.env[\"uri.query\"] and string.find(lighty.env[\"uri.query\"], \".*s=.*\"))\n    user_cookie = lighty.request[\"Cookie\"] or \"no_cookie_here\"\n    cookie_condition = not (string.find(user_cookie, \".*comment_author.*\") or (string.find(user_cookie, \".*wordpress.*\") and not string.find(user_cookie,\"wordpress_test_cookie\")))\n\n    if (query_condition and cookie_condition) then\n        accept_encoding = lighty.request[\"Accept-Encoding\"] or \"no_acceptance\"\n\n        if (string.find(accept_encoding, \"gzip\")) then\n            if not serve_gzip(cached_page) then \n                serve_html(cached_page) \n            end\n        else\n            serve_html(cached_page)\n        end\n        --log('cache-hit: ' .. cached_page)\n    end\nelse\n    --log('cache-miss: ' .. cached_page)\nend\n<\/code><\/pre>\n<h1>Configuring your vhost in lighttpd for WordPress redirects and direct cache serves without php.<\/h1>\n<p>Then on your vhost configuration in lighttpd.conf add the following towards the end.<br \/>\n(Fix paths if you have to)<\/p>\n<pre><code>var.wp_blog = 1\n\nmagnet.attract-physical-path-to = ( server.document-root + \"\/rewrite.lua\" )\n\nurl.rewrite-if-not-file = (\n   \"^\/(wp-.+).*\/?\" =&gt; \"$0\",\n   \"^\/(sitemap.xml)\" =&gt; \"$0\",\n   \"^\/(xmlrpc.php)\" =&gt; \"$0\",\n   \"^\/(.+)\/?$\" =&gt; \"\/index.php\/$1\"\n  )\n\n<\/code><\/pre>\n<p>Restart your lighttpd <code>sudo service lighttpd restart<\/code><\/p>\n<p>Now watch how your PHP processes breathe a lot better and you page loads are insanely faster.<\/p>\n<p>You&#8217;re welcome \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Optimizing Your WordPress Cache Loads in Lighttpd. If you don&#8217;t configure your wordpress virtual host properly in lighttpd, your wordpress cache will still make use of PHP. Wouldn&#8217;t it be nice if all those cached requests were served directy from the webserver as the static files that they are, bypassing the CPU\/memory load PHP can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,30,43,1],"tags":[1501,296,618,1500,1182,925,1117,1502],"class_list":["post-3517","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-linux","category-uncategorized","tag-cache","tag-configuration","tag-lighttpd","tag-lua","tag-php","tag-speed","tag-wordpress","tag-wp-supercache"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-UJ","jetpack-related-posts":[{"id":3839,"url":"https:\/\/www.gubatron.com\/blog\/nginx-server-configuration-for-a-wordpress-instance-served-from-a-urls-subdirectory\/","url_meta":{"origin":3517,"position":0},"title":"nginx server configuration for a wordpress instance served from a URL&#8217;s subdirectory","author":"gubatron","date":"November 14, 2019","format":false,"excerpt":"You want to serve a wordpress instance on a website's domain url but not at the path's root, you want it under a sub-directory, for example \"blog\", the same as this blog: https:\/\/www.gubatron.com\/blog\u00a0 Here's how my NGINX's server block for 'www.gubatron.com' looks like at the moment (https\/ssl hasn't been configured\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2593,"url":"https:\/\/www.gubatron.com\/blog\/lighttpd-allow-access-control-allow-origin-headers-on-the-server-status-page\/","url_meta":{"origin":3517,"position":1},"title":"lighttpd, allow &#8220;Access-Control-Allow-Origin:*&#8221; headers on the server status page","author":"gubatron","date":"November 29, 2011","format":false,"excerpt":"Maybe there's someone out there who needs to read the output of lighttpd's status for monitoring purpose like me tonight, and also, like me, you want to do this using JavaScript, but your browser gives you this nasty error: XMLHttpRequest cannot load http:\/\/otherSubdomain.server.com\/lighttpd-status-url-you-have-configured. Origin http:\/\/requestingSubdomain.server.com is not allowed by Access-Control-Allow-Origin.\u2026","rel":"","context":"In &quot;AJAX&quot;","block_context":{"text":"AJAX","link":"https:\/\/www.gubatron.com\/blog\/category\/ajax\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3591,"url":"https:\/\/www.gubatron.com\/blog\/upgrading-your-wordpress-blog-to-php-7-0-on-ubuntu-xenial\/","url_meta":{"origin":3517,"position":2},"title":"Upgrading your wordpress blog to PHP 7.0 on Ubuntu Xenial","author":"gubatron","date":"September 7, 2016","format":false,"excerpt":"If you're about to upgrade your Ubuntu server to 16.04 (Xenial) you might want to take advantage of the new PHP 7.0 which is as fast or more than Facebook's HHVM, or perhaps during the upgrade process a few things may have broken and perhaps that's why you're here Make\u2026","rel":"","context":"In &quot;Linux&quot;","block_context":{"text":"Linux","link":"https:\/\/www.gubatron.com\/blog\/category\/linux\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3376,"url":"https:\/\/www.gubatron.com\/blog\/what-to-do-when-lighttpd-wont-start-and-wont-give-out-any-error-output\/","url_meta":{"origin":3517,"position":3},"title":"What to do when lighttpd won&#8217;t start and won&#8217;t give out any error output?","author":"gubatron","date":"February 4, 2015","format":false,"excerpt":"So you upgraded your server, or just all of a sudden you try to start lighttpd, it says the server started ok, but you check and there's no lighttpd process. You then go after your error log files, and nothing... what the fuck is happening? try this to attempt to\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3899,"url":"https:\/\/www.gubatron.com\/blog\/how-to-suppress-useless-mod_openssl-lighttpd-error-messages-from-var-log-syslog-in-ubuntu\/","url_meta":{"origin":3517,"position":4},"title":"[linux\/ubuntu] How to suppress useless mod_openssl\/lighttpd error messages from appearing in \/var\/log\/syslog","author":"gubatron","date":"August 7, 2020","format":false,"excerpt":"Sometimes you have a bunch of useless errors creating unnecessary disk I\/O on your server, disk I\/O that should be used towards serving your user's requests efficiently. In this case a site running on lighttpd keeps logging several times per second the following message, creating too much noise and making\u2026","rel":"","context":"In &quot;Linux&quot;","block_context":{"text":"Linux","link":"https:\/\/www.gubatron.com\/blog\/category\/linux\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":913,"url":"https:\/\/www.gubatron.com\/blog\/how-to-filter-logs-in-lighttpd\/","url_meta":{"origin":3517,"position":5},"title":"How to filter logs in lighttpd","author":"gubatron","date":"October 27, 2008","format":false,"excerpt":"I usually don't keep lighttpd access logs turned on to avoid writing for every read, but there are times when you need to monitor what's going on, and you'd like to have a high signal-to-noise ratio so it might be convenient to ignore all requests to .gif, .png, .jpg, .css,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.lighttpd.net\/light_logo.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3517"}],"version-history":[{"count":11,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3517\/revisions"}],"predecessor-version":[{"id":3533,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3517\/revisions\/3533"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}