Tuning the NGINX - or how to make your website faster

Tuning the NGINX - or how to make your website faster

This is a talk first held at the Typo3 Developer Days 2019.
Watch the talk here: https://www.youtube.com/watch?v=czNKukCaA8M
Have a look at the configs here: https://github.com/foppelfb/nginx-talk

E789161a9b29490b909728b6e4811871?s=128

Frank Berger

August 02, 2019
Tweet

Transcript

  1. TUNING THE NGINX or 
 how to make your Website

    faster
  2. • Frank Berger (Greybeard) • Head of Engineering
 B-FACTOR Sudhaus7

    • started 1996 as Unix Systemadministrator who also programs (aka „Devops“) Sudhaus7 A SHORT NOTE ABOUT ME
  3. FOREWORD Why NGINX? Sudhaus7

  4. Not optimised yet… It’s on the Todo list, I promise

    SHINY NEW WEBSITE Sudhaus7
  5. Then the URL gets posted here Sudhaus7

  6. Our 16 Core Server just vanishes Sudhaus7

  7. Sudhaus7 WHAT’S ON THE SERVER? • Debian Linux • PHP

    7.2 (opcache enabled) • MariaDB 10.4 (not tuned) 
 • Nginx 1.14 • Typo3 9.5.8
 • Shiny new Website, with lots of uncached USER_INT - because the Editor can’t wait to see the changes online
 (and we have lots of user input)
  8. Testing with siege 100 or more concurrent requests for 30

    seconds on a list of random urls LET’S TEST THE SERVER Sudhaus7
  9. worker_processes 3; worker_rlimit_nofile 40000; pcre_jit on; events { use epoll;

    worker_connections 1024; multiaccept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; } server { open_file_cache max=10000 inactive=30s; open_file_cache_valid 2m; open_file_cache_min_uses 2; open_file_cache_errors on; } WHAT CAN WE DO TO IMPROVE? Sudhaus7
  10. http { fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=TYPO3:10m inactive=2m; fastcgi_cache_key "$scheme$request_method$host$uri$args"; fastcgi_cache_use_stale

    error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; } location ~ [^/]\.php(/|$) { set $skip_cache 0; if ($request_method = POST) { set $skip_cache 1; } if ($request_uri ~* "/typo3/|/index.php?eID=") { set $skip_cache 1; } if ($http_cookie ~* "be_typo_user|fe_typo_user") { set $skip_cache 1; } fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache TYPO3; fastcgi_cache_valid 200 10s; add_header X-FastCGI-Cache $upstream_cache_status; } Sudhaus7 NOT GOOD ENOUGH, WHAT ELSE?
  11. http { fastcgi_cache_use_stale updating error timeout invalid_header http_500; fastcgi_cache_background_update on;

    fastcgi_cache_lock on; } Sudhaus7 GREAT! BUT CACHE WARMUP PROBLEMS
  12. Sudhaus7 THE PROBLEM WITH THIS KIND OF CACHING • Everything

    gets cached for the specified time, even uncached content • Cache-Control Headers are ignored • There is no easy way to purge the cache 
 for a specific page
  13. If we could just compartmentalise different parts of the Webpage

    based on their lifetime Sudhaus7
  14. <!—#include virtual=„“ —> (or how webpages were made dynamic in

    the early 1990’s) Sudhaus7 ENTER SSI
  15. config.sendCacheHeaders = 1 composer require ichhabrecht/intcache Replaces USER_INT with <!—#include

    virtual=„“—> to have a pageType call render the uncached content in a separate call Supports as well ESI (Varnish and Cloud flare) and AJAX Sudhaus7 EXTENSION FOR TYPO3
  16. http { fastcgi_ignore_headers Set-Cookie; } location ~ [^/]\.php(/|$) { ssi

    on; set $cachekey „$scheme$host$request_uri"; if ($args ~* "tx_intcache") { set $cachekey "$scheme$host$uri$args"; } fastcgi_cache_key $cachekey; } Sudhaus7 LET’S TRY
  17. Sudhaus7 ADVANTAGES/EFFECTS • Cache-Control Headers are recognised 
 and honoured

    by nginx
 • Uncached Content will be delivered directly, 
 but the surrounding page will stay cached • Extensions can send their own Cache-Control 
 headers based on their needs • Extensions can make better use of the Typo3 Cache 
 • Multiple uncached extensions in a 
 single Page can be handled separately
  18. • The NGINX Cache could be stored in a Redis

    cache
 enabling Failover Servers or Multiple Frontend Servers 
 using the same cached content • using the proxy_ variations of the configuration commands 
 can be used to set up a distributed cloud system • or using a docker-based backend, with the same 
 advantages as shown in this talk • The SSI technology can be used as well to include 
 Widgets from other frameworks or even technologies. Sudhaus7 OTHER COOL STUFF
  19. Questions and Discussions https://github.com/foppelfb/nginx-talk.git Sudhaus7

  20. Frank Berger Sudhaus7 (eine Marke der B-FACTOR Gmbh) fberger@sudhaus7.de Twitter:

    @foppelfb https://github.com/foppelfb Sudhaus7 THANK YOU!