Slide 1

Slide 1 text

TUNING THE NGINX or 
 how to make your Website faster

Slide 2

Slide 2 text

• 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

Slide 3

Slide 3 text

FOREWORD Why NGINX? Sudhaus7

Slide 4

Slide 4 text

Not optimised yet… It’s on the Todo list, I promise SHINY NEW WEBSITE Sudhaus7

Slide 5

Slide 5 text

Then the URL gets posted here Sudhaus7

Slide 6

Slide 6 text

Our 16 Core Server just vanishes Sudhaus7

Slide 7

Slide 7 text

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)

Slide 8

Slide 8 text

Testing with siege 100 or more concurrent requests for 30 seconds on a list of random urls LET’S TEST THE SERVER Sudhaus7

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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?

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

If we could just compartmentalise different parts of the Webpage based on their lifetime Sudhaus7

Slide 14

Slide 14 text

(or how webpages were made dynamic in the early 1990’s) Sudhaus7 ENTER SSI

Slide 15

Slide 15 text

config.sendCacheHeaders = 1 composer require ichhabrecht/intcache Replaces USER_INT with 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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

• 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

Slide 19

Slide 19 text

Questions and Discussions https://github.com/foppelfb/nginx-talk.git Sudhaus7

Slide 20

Slide 20 text

Frank Berger Sudhaus7 (eine Marke der B-FACTOR Gmbh) [email protected] Twitter: @foppelfb https://github.com/foppelfb Sudhaus7 THANK YOU!