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
CACHED

HTML
Pep
CACHED HTML
DRUPAL 7
Pros:

• Full page caching is available
Cons:

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

• 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 PLUGIN
function 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.
DrupalCon Barcelona 2015

DrupalCon Barcelona 2015

  • 2.
    NGINX/VARNISH FULL PAGE CACHINGFOR DRUPAL DANIEL KANCHEV Twitter: @dvkanchev SITEGROUND.COM
  • 3.
    WHY YOU NEEDFULL PAGE CACHING ?
  • 4.
  • 5.
    WHAT EXACTLY ISFULL PAGE CACHING?
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
    DRUPAL 7 Pros:
 • Fullpage caching is available Cons:
 • It is not Nginx/Varnish friendly • It does not offer easy cache invalidation • Not enabled by default
  • 16.
    DRUPAL 8 Pros:
 • Reverseproxy (Nginx/Varnish) friendly full page caching • Easy to invalidate • Enabled by default Cons:
 • None I can think of :)
  • 17.
  • 18.
    DRUPAL 7 +VARNISH/NGINX • Install Varnish/Nginx • Create a configuration file for Varnish/Nginx • Install a plugin which will invalidate the cache
  • 19.
    DRUPAL 7 +VARNISH/NGINX • Install Varnish/Nginx • Create a configuration file for Varnish/Nginx • Install a plugin which will invalidate the cache
  • 20.
    SITEGROUND SOLUTION • Ourown plugin for Drupal 7 - Cache auto invalidation - URL cache blacklisting • Custom Nginx configuration - Suitable for shared hosting servers - Cached copies are kept in RAM
  • 21.
    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
  • 22.
    DRUPAL 7 PLUGIN functionsupercacher_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(); }
  • 23.
    SOME MORE CODEFROM THE DRUPAL 7 PLUGIN
  • 24.
  • 25.
    ROADMAP FOR OUR SUPERCACHER •Support For Drupal 8 • Use cache tags/meta data for invalidation • Implement user based caching
  • 26.
    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
  • 27.
    FRIDAY SPRINTS • Sprintwith 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.