From fd803b76e17f89d69a4a6fe9eed64590aea56ed5 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Mon, 30 Sep 2024 22:16:55 +0300 Subject: [PATCH] doc: add config-template example --- doc/examples/README.md | 1 + doc/examples/config-template/README.md | 113 ++++++++++++++++++ .../conf/image-entry/80-my-caches.envsh | 13 ++ .../image-entry/81-angie-console-light.sh | 3 + .../config-template/conf/j2cfg/my-caches.yml | 67 +++++++++++ .../conf/site/http-my-cache.conf.j2 | 81 +++++++++++++ .../conf/site/my-caches.txt.j2 | 3 + .../config-template/docker-compose.yml | 16 +++ 8 files changed, 297 insertions(+) create mode 100644 doc/examples/config-template/README.md create mode 100755 doc/examples/config-template/conf/image-entry/80-my-caches.envsh create mode 100755 doc/examples/config-template/conf/image-entry/81-angie-console-light.sh create mode 100644 doc/examples/config-template/conf/j2cfg/my-caches.yml create mode 100644 doc/examples/config-template/conf/site/http-my-cache.conf.j2 create mode 100644 doc/examples/config-template/conf/site/my-caches.txt.j2 create mode 100644 doc/examples/config-template/docker-compose.yml diff --git a/doc/examples/README.md b/doc/examples/README.md index 4255cc8..b4497fe 100644 --- a/doc/examples/README.md +++ b/doc/examples/README.md @@ -5,3 +5,4 @@ - [print env via NJS](njs/README.md) - [print env via Perl](perl/README.md) - [SSL with subdomains](ssl/README.md) +- [generating config with templates](config-template/README.md) diff --git a/doc/examples/config-template/README.md b/doc/examples/config-template/README.md new file mode 100644 index 0000000..1d460ee --- /dev/null +++ b/doc/examples/config-template/README.md @@ -0,0 +1,113 @@ +# generating config with templates + +configuration: + +```nginx +map $uri $to_proxy_uri +{ + ~^/[^/]+/(.*)$ $1; +} + +{%- for h in (j2cfg.my_caches or []) %} +proxy_cache_path + /angie/my-cache/{{ h.name }} + keys_zone={{ h.name }}:20m + levels=1:2 inactive={{ h.max_time }}; +{%- endfor %} + +server { + listen 8888; + + location / { return 204; } + + {%- for h in (j2cfg.my_caches or []) %} + location /{{ h.name }}/ { + proxy_pass {{ h.uri }}/$to_proxy_uri$is_args$args; + + proxy_cache {{ h.name }}; + + expires {{ h.valid_time }}; + + proxy_cache_valid 200 {{ h.valid_time }}; + proxy_cache_valid any 30s; + } + {%- endfor %} +} +``` + +--- + +site configuration (via `j2cfg/my-caches.yml`): + +```yml +my_caches: + - name: proxy_apt_debian + uri: https://deb.debian.org/debian + valid_time: 180m + max_time: 1440h + - name: proxy_apt_debian_security + uri: https://deb.debian.org/debian-security + valid_time: 180m + max_time: 1440h +## and so on ... +``` + +--- + +docker-compose.yml: + +```yml +version: "3.8" + +services: + + my-cache: + container_name: my-cache + image: docker.io/rockdrilla/angie-conv:v0.0.2 + restart: always + privileged: true + stop_grace_period: 15s + network_mode: host + volumes: + - "./conf/image-entry:/image-entry:ro" + - "./conf/j2cfg:/angie/j2cfg:ro" + - "./conf/site:/angie/site:ro" + - "./cache:/angie/my-cache" +``` + +--- + +final configuration looks like this: + +```nginx +proxy_cache_path + /angie/my-cache/proxy_apt_debian + keys_zone=proxy_apt_debian:20m + levels=1:2 inactive=1440h; + +# ... + +server { +# ... + + location /proxy_apt_debian/ { + proxy_pass https://deb.debian.org/debian/$to_proxy_uri$is_args$args; + + proxy_cache proxy_apt_debian; + + expires 180m; + + proxy_cache_valid 200 180m; + proxy_cache_valid any 30s; + } + +# ... +} +``` + +--- + +Test URI e.g. with `curl`: +```sh +curl -v http://localhost:8888/proxy_apt_debian/dists/bookworm/main/binary-all/Release +``` diff --git a/doc/examples/config-template/conf/image-entry/80-my-caches.envsh b/doc/examples/config-template/conf/image-entry/80-my-caches.envsh new file mode 100755 index 0000000..3e06ee8 --- /dev/null +++ b/doc/examples/config-template/conf/image-entry/80-my-caches.envsh @@ -0,0 +1,13 @@ +#!/bin/sh + +unset d +d="${target_root}/cache/my-cache" +[ -d "$d" ] || install_userdir "$d" + +unset p +while read -r p ; do + [ -n "$p" ] || continue + [ -d "$d/$p" ] || install_userdir "$d/$p" +done < /etc/angie/site.d/my-caches.txt + +unset d p diff --git a/doc/examples/config-template/conf/image-entry/81-angie-console-light.sh b/doc/examples/config-template/conf/image-entry/81-angie-console-light.sh new file mode 100755 index 0000000..c70eabb --- /dev/null +++ b/doc/examples/config-template/conf/image-entry/81-angie-console-light.sh @@ -0,0 +1,3 @@ +#!/bin/sh +apt-install.sh angie-console-light +apt-clean.sh \ No newline at end of file diff --git a/doc/examples/config-template/conf/j2cfg/my-caches.yml b/doc/examples/config-template/conf/j2cfg/my-caches.yml new file mode 100644 index 0000000..2bcc7bd --- /dev/null +++ b/doc/examples/config-template/conf/j2cfg/my-caches.yml @@ -0,0 +1,67 @@ +my_caches: + - name: proxy_apt_debian + uri: https://deb.debian.org/debian + valid_time: 180m + max_time: 1440h + - name: proxy_apt_debian_security + uri: https://deb.debian.org/debian-security + valid_time: 180m + max_time: 1440h + + - name: proxy_apt_postgresql + uri: https://apt.postgresql.org/pub/repos/apt + valid_time: 180m + max_time: 1440h + - name: proxy_apt_citus_debian + uri: https://repos.citusdata.com/community/debian + valid_time: 180m + max_time: 1440h + - name: proxy_apt_citus + uri: https://repos.citusdata.com/community + valid_time: 180m + max_time: 1440h + + - name: proxy_apt_ubuntu + uri: https://mirror.yandex.ru/ubuntu + valid_time: 180m + max_time: 1440h + - name: proxy_apt_citus_ubuntu + uri: https://repos.citusdata.com/community/ubuntu + valid_time: 180m + max_time: 1440h + - name: proxy_apt_docker_ubuntu + uri: https://download.docker.com/linux/ubuntu + valid_time: 180m + max_time: 1440h + + - name: proxy_go + uri: https://proxy.golang.org + valid_time: 120m + max_time: 1440h + - name: proxy_go_sum + uri: https://sum.golang.org + valid_time: 120m + max_time: 1440h + - name: proxy_go_dev + uri: https://go.dev + valid_time: 120m + max_time: 1440h + + - name: proxy_npm + uri: https://registry.npmjs.org + valid_time: 120m + max_time: 1440h + + - name: proxy_pypi + uri: https://pypi.org + valid_time: 120m + max_time: 1440h + + - name: proxy_crates + uri: https://crates.io + valid_time: 120m + max_time: 1440h + - name: proxy_crates_index + uri: https://index.crates.io + valid_time: 120m + max_time: 1440h diff --git a/doc/examples/config-template/conf/site/http-my-cache.conf.j2 b/doc/examples/config-template/conf/site/http-my-cache.conf.j2 new file mode 100644 index 0000000..63a054f --- /dev/null +++ b/doc/examples/config-template/conf/site/http-my-cache.conf.j2 @@ -0,0 +1,81 @@ +map $uri + $to_proxy_uri +{ + volatile; + + ~^/[^/]+/(.*)$ $1; +} + +map $request_method + $to_proxy_method +{ + default GET; + ## already handled by "proxy_cache_convert_head on;" (default setting) + # HEAD GET; + OPTIONS OPTIONS; +} + +## quirks + +chunked_transfer_encoding off; +proxy_method $to_proxy_method; +proxy_ignore_client_abort on; +proxy_ignore_headers Cache-Control Expires Set-Cookie Vary X-Accel-Buffering X-Accel-Expires X-Accel-Limit-Rate; + +## tuning + +proxy_cache_key $to_proxy_uri$is_args$args; + +proxy_cache_lock on; +proxy_cache_lock_age 20s; +proxy_cache_lock_timeout 25s; +proxy_cache_use_stale error timeout invalid_header updating http_429 http_500 http_502 http_503 http_504; +proxy_cache_revalidate on; + +{%- for h in (j2cfg.my_caches or []) %} +proxy_cache_path + /angie/my-cache/{{ h.name }} + keys_zone={{ h.name }}:20m + levels=1:2 inactive={{ h.max_time }}; +{%- endfor %} + +server { + listen 8888; + + location / { return 204; } + + location /console/ { + # allow 127.0.0.0/8; + # deny all; + + auto_redirect on; + + alias /usr/share/angie-console-light/html/; + index index.html; + + location /console/api/ + { + access_log off; + api /status/; + } + + location /console/api/config/ + { + access_log off; + api /config/; + } + } + + {%- for h in (j2cfg.my_caches or []) %} + location /{{ h.name }}/ { + proxy_pass {{ h.uri }}/$to_proxy_uri$is_args$args; + + proxy_cache {{ h.name }}; + + expires {{ h.valid_time }}; + + proxy_cache_valid 200 {{ h.valid_time }}; + proxy_cache_valid any 30s; + } + {%- endfor %} +} diff --git a/doc/examples/config-template/conf/site/my-caches.txt.j2 b/doc/examples/config-template/conf/site/my-caches.txt.j2 new file mode 100644 index 0000000..71d16ce --- /dev/null +++ b/doc/examples/config-template/conf/site/my-caches.txt.j2 @@ -0,0 +1,3 @@ +{%- for h in (j2cfg.my_caches or []) %} +{{ h.name }} +{%- endfor %} diff --git a/doc/examples/config-template/docker-compose.yml b/doc/examples/config-template/docker-compose.yml new file mode 100644 index 0000000..7798e34 --- /dev/null +++ b/doc/examples/config-template/docker-compose.yml @@ -0,0 +1,16 @@ +version: "3.8" + +services: + + my-cache: + container_name: my-cache + image: docker.io/rockdrilla/angie-conv:v0.0.2 + restart: always + privileged: true + stop_grace_period: 15s + network_mode: host + volumes: + - "./conf/image-entry:/image-entry:ro" + - "./conf/j2cfg:/angie/j2cfg:ro" + - "./conf/site:/angie/site:ro" + - "./cache:/angie/my-cache"