Skip to content

Commit

Permalink
Add cache
Browse files Browse the repository at this point in the history
  • Loading branch information
thueske committed Jun 30, 2024
1 parent d5df77a commit 86cadfd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ MARIADB_TMP_TABLE_SIZE=
MARIADB_MAX_HEAP_TABLE_SIZE=
SSH_USERNAME=
SSH_PORT=
COMPOSE_PROFILES=
COMPOSE_PROFILES=
PERL5LIB=/usr/lib/perl5/vendor_perl/aarch64-linux-thread-multi
40 changes: 38 additions & 2 deletions conf/nginx/default.conf.template
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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$ {
Expand All @@ -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;
}
}
5 changes: 5 additions & 0 deletions conf/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
13 changes: 0 additions & 13 deletions conf/nginx/purge.pm
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 86cadfd

Please sign in to comment.