From 128fbc825881b9e150dd1adaedb2072603f9e655 Mon Sep 17 00:00:00 2001
From: psy <psy@cccv.de>
Date: Mon, 12 Jun 2023 21:45:23 +0200
Subject: [PATCH] add caching feature

---
 defaults/main.yml        |  6 ++++++
 tasks/main.yml           | 10 ++++++++++
 templates/caches.conf.j2 |  6 ++++++
 3 files changed, 22 insertions(+)
 create mode 100644 templates/caches.conf.j2

diff --git a/defaults/main.yml b/defaults/main.yml
index 4cae7d4..2ddaf20 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -12,6 +12,7 @@ nginx:
   vhosts: {}
   streams: {}
   maps: {}
+  caches: {}
   resolver:
     - 8.8.8.8
     - 8.8.4.4
@@ -57,6 +58,11 @@ nginx_streams_defaults:
   proxy_connect_timeout: "2s"
   proxy_next_upstream_tries: 2
 
+nginx_caches_defaults:
+  keys_zone_size: "10m"
+  cache_size: "1g"
+  inactive_time: "10m"
+
 nginx_forcessl_vhost:
   "https-redirect":
     listen:
diff --git a/tasks/main.yml b/tasks/main.yml
index 3f3dd30..043be4c 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -42,6 +42,15 @@
   notify:
   - restart nginx
 
+- name: create cache directories
+  with_dict: "{{ nginx.caches }}"
+  file:
+    path: "/var/cache/nginx/{{ item.key }}"
+    state: directory
+    mode: '0755'
+    owner: www-data
+    recurse: True
+
 - name: execute ssl template
   template:
     src: ssl_files.conf.j2
@@ -58,6 +67,7 @@
     - upstreams.conf
     - proxy.conf
     - maps.conf
+    - caches.conf
   template:
     src: "{{ item }}.j2"
     dest: "/etc/nginx/conf.d/{{ item }}"
diff --git a/templates/caches.conf.j2 b/templates/caches.conf.j2
new file mode 100644
index 0000000..12dee17
--- /dev/null
+++ b/templates/caches.conf.j2
@@ -0,0 +1,6 @@
+{% for cache in nginx.caches %}
+{% set c = {}|combine(nginx_caches_defaults, nginx.caches[cache], recursive=True) %}
+# {{ cache }}
+proxy_cache_path /var/cache/nginx/{{ cache }} levels=1:2 keys_zone={{ cache }}:{{ c.keys_zone_size }} max_size={{ c.cache_size }} inactive={{ c.inactive_time }} use_temp_path=off;
+
+{% endfor %}
\ No newline at end of file
-- 
GitLab