From 86cadfd4a22fbaeb5ec2188e4bfe3c789395c81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=BCske?= Date: Sun, 30 Jun 2024 07:28:45 +0200 Subject: [PATCH] Add cache --- .env.example | 3 ++- conf/nginx/default.conf.template | 40 ++++++++++++++++++++++++++++++-- conf/nginx/nginx.conf | 5 ++++ conf/nginx/purge.pm | 13 ----------- docker-compose.yml | 4 ++-- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index 2d04d91..6d434f6 100644 --- a/.env.example +++ b/.env.example @@ -17,4 +17,5 @@ MARIADB_TMP_TABLE_SIZE= MARIADB_MAX_HEAP_TABLE_SIZE= SSH_USERNAME= SSH_PORT= -COMPOSE_PROFILES= \ No newline at end of file +COMPOSE_PROFILES= +PERL5LIB=/usr/lib/perl5/vendor_perl/aarch64-linux-thread-multi \ No newline at end of file diff --git a/conf/nginx/default.conf.template b/conf/nginx/default.conf.template index 2c50f01..ea93083 100644 --- a/conf/nginx/default.conf.template +++ b/conf/nginx/default.conf.template @@ -1,10 +1,31 @@ perl_modules ${PERL5LIB}; perl_require purge.pm; +fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=WORDPRESS:16m max_size=512m inactive=2h use_temp_path=off; +fastcgi_cache_key "$scheme$request_method$host$request_uri"; +fastcgi_ignore_headers Cache-Control Expires Set-Cookie; + server { listen 80; index index.php index.htm index.html; + set $skip_cache 0; + + if ($request_method = POST) { + set $skip_cache 1; + } + if ($query_string != "") { + set $skip_cache 1; + } + + if ($request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|^/feed/*|/tag/.*/feed/*|index.php|/.*sitemap.*\.(xml|xsl)|/checkout/|/my-account/") { + set $skip_cache 1; + } + + if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|edd_items_in_cart|woocommerce_items_in_cart") { + set $skip_cache 1; + } + root /var/www/html; error_log /var/log/nginx/error.log; @@ -22,9 +43,15 @@ server { } location /purge-all { - allow 127.0.0.1; + if ($purge_method) { + allow 127.0.0.1; + allow 10.0.0.0/8; + allow 172.16.0.0/12; + allow 192.168.0.0/16; + deny all; + perl purge::purge; + } deny all; - perl purge::purge; } location ~ \.php$ { @@ -38,5 +65,14 @@ server { set_real_ip_from 172.16.0.0/12; real_ip_header X-Forwarded-For; real_ip_recursive on; + fastcgi_cache WORDPRESS; + fastcgi_cache_background_update on; + fastcgi_cache_valid 200 301 302 2h; + fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503; + fastcgi_cache_min_uses 1; + fastcgi_cache_lock on; + fastcgi_cache_bypass $skip_cache; + fastcgi_no_cache $skip_cache; + add_header X-FastCGI-Cache $upstream_cache_status; } } \ No newline at end of file diff --git a/conf/nginx/nginx.conf b/conf/nginx/nginx.conf index 057e2ce..fa78599 100644 --- a/conf/nginx/nginx.conf +++ b/conf/nginx/nginx.conf @@ -19,6 +19,11 @@ http { include /etc/nginx/mime.types; default_type application/octet-stream; + map $request_method $purge_method { + default 0; + PURGE 1; + } + map $http_accept $webp_suffix { default ""; "~*webp" ".webp"; diff --git a/conf/nginx/purge.pm b/conf/nginx/purge.pm index a03c687..078125a 100644 --- a/conf/nginx/purge.pm +++ b/conf/nginx/purge.pm @@ -1,22 +1,9 @@ package purge; -# Füge dynamisch den Architektur-spezifischen Pfad hinzu -use Config; -use File::Spec; - -BEGIN { - my $archname = $Config{archname}; - my $vendorlib = File::Spec->catdir($Config{vendorlibexp}, $archname); - push @INC, $vendorlib; -} - use nginx; sub purge { - # Ein Systembefehl wird ausgeführt, um den Cache zu leeren system "rm -rf /etc/nginx/cache/*"; - - # HTTP_NO_CONTENT ist eine Konstante aus dem Nginx-Modul und bedeutet 204 No Content return HTTP_NO_CONTENT; } diff --git a/docker-compose.yml b/docker-compose.yml index 551c201..bf774b7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,10 +37,10 @@ services: environment: - TZ=Europe/Berlin - COMPOSE_PROJECT_NAME - - PERL5LIB=/usr/lib/perl5/vendor_perl/aarch64-linux-thread-multi + - PERL5LIB volumes: - $PWD/data/web:/var/www/html - - $PWD/conf/nginx/purge.pm:/usr/lib/perl5/vendor_perl/aarch64-linux-thread-multi/purge.pm + - $PWD/conf/nginx/purge.pm:${PERL5LIB}/purge.pm - $PWD/conf/nginx/nginx.conf:/etc/nginx/nginx.conf - $PWD/conf/nginx/7g.conf:/etc/nginx/conf.d/7g.conf - $PWD/conf/nginx/7g-firewall.conf:/etc/nginx/conf.d/7g-firewall.conf