28

DrupalCon Barcelona 2015

Embed Size (px)

Citation preview

NGINX/VARNISH FULL PAGE CACHING FOR DRUPAL

DANIEL KANCHEV

Twitter: @dvkanchev SITEGROUND.COM

WHY YOU NEED FULL PAGE CACHING ?

CODE RED WHILE OUT AT LUNCH

WHAT EXACTLY IS FULL PAGE CACHING?

Pep

Pep

Pep

Pep

Apache

Pep

Apache

PHP

Pep

Apache

PHP

MySQL

Pep

Apache

PHP

MySQL

HTML

Pep

CACHEDHTML

Pep

CACHED HTML

DRUPAL 7Pros:

• Full page caching is available

Cons:

• It is not Nginx/Varnish friendly • It does not offer easy cache invalidation • Not enabled by default

DRUPAL 8Pros:

• Reverse proxy (Nginx/Varnish) friendly full page caching • Easy to invalidate • Enabled by default

Cons:

• None I can think of :)

Pep

Apache PHP

MySQL

Nginx Varnish

Reverse Proxy Backend Server

REVERSE PROXY?

DRUPAL 7 + VARNISH/NGINX

• Install Varnish/Nginx

• Create a configuration file for Varnish/Nginx

• Install a plugin which will invalidate the cache

DRUPAL 7 + VARNISH/NGINX

• Install Varnish/Nginx

• Create a configuration file for Varnish/Nginx

• Install a plugin which will invalidate the cache

SITEGROUND SOLUTION• Our own plugin for Drupal 7

- Cache auto invalidation - URL cache blacklisting

• Custom Nginx configuration

- Suitable for shared hosting servers - Cached copies are kept in RAM

map $http_cookie $nocache_cookie_drupal { ~*(^|\;\s*)(drupalSGCacheBypass=1)(\;|$) 1; ~*Drupal.visitor.name 1; default 0; } map $http_cookie $skipcache_cookie_drupal { ~*(^|\;\s*)(drupalSGCacheBypass=1)(\;|$) 1; ~*Drupal.visitor.name 1; default 0; } map $request_uri $nocache_uri_drupal { ~*^/admin/content/backup_migrate/export 1; ~*^/(status\.php$|update\.php|install\.php) 1; ~*^/(admin|admin/.*$|user|user/.*$|info/.*$|flag/.*$|ajax/.*$|aha/.*$) 1; default 0; } map $request_uri $skipcache_uri_drupal { ~*^/admin/content/backup_migrate/export 1; ~*^/(status\.php$|update\.php|install\.php) 1; ~*^/(admin|admin/.*$|user|user/.*$|info/.*$|flag/.*$|ajax/.*$|aha/.*$) 1; default 0; }

NGINX CONF

DRUPAL 7 PLUGINfunction supercacher_page_alter(&$page) { if ( !variable_get('supercacher_enabled', 0) || supercacher_isUrlBlacklisted()) { drupal_add_http_header('X-Cache-Enabled', 'False'); return; }

drupal_add_http_header('X-Cache-Enabled', 'True');

// Logged In Users if ( user_is_logged_in() || ( ! empty( $_POST['op'] ) && 'Log In' === $_POST['op'] ) ) { setcookie( 'drupalSGCacheBypass', 1, time() + 100 * 60, '/' ); } elseif ( !user_is_logged_in() && (!isset($_COOKIE['drupalSGCacheBypass']) || ($_COOKIE['drupalSGCacheBypass'] != 1))) { setcookie( 'drupalSGCacheBypass', 0, time() - 3600, '/' ); } //dirty hacks: if (isset($_POST['form_id']) && $_POST['form_id'] == 'system_theme_settings') supercacher_purge(); }

SOME MORE CODE FROM THE DRUPAL 7 PLUGIN

OUR PLUGIN

http://download.siteground.com/drupal/drupalSGCache.zip

ROADMAP FOR OUR SUPERCACHER

• Support For Drupal 8

• Use cache tags/meta data for invalidation

• Implement user based caching

RESOURCES• https://groups.drupal.org/nginx • http://wiki.nginx.org/Drupal • https://www.drupal.org/node/1054886 • https://www.drupal.org/project/varnish • http://wimleers.com/blog/drupal-8-page-caching-enabled-by-default

• http://wimleers.com/blog/renderviz-prototype

FRIDAY SPRINTS• Sprint with the Community on Friday.

• We have tasks for every skillset.

• Mentors are available for new contributors.

• An optional Friday morning workshop for first-time sprinters will help you get set up.

• Follow @drupalmentoring.