From a4a23e366a1e8f5300c7d548dca3cc6a7fef7172 Mon Sep 17 00:00:00 2001 From: bartbutenaers Date: Sat, 8 Mar 2025 21:47:50 +0100 Subject: [PATCH] Base path via environment variable --- .../rootfs/etc/s6-overlay/s6-rc.d/nginx/run | 10 ++++++++++ .../main/rootfs/usr/local/nginx/conf/nginx.conf | 7 +++++-- .../rootfs/usr/local/nginx/get_base_path.py | 10 ++++++++++ .../usr/local/nginx/templates/base_path.gotmpl | 17 +++++++++++++++++ .../local/nginx/templates/web_manifest.gotmpl | 13 +++++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 docker/main/rootfs/usr/local/nginx/get_base_path.py create mode 100644 docker/main/rootfs/usr/local/nginx/templates/base_path.gotmpl create mode 100644 docker/main/rootfs/usr/local/nginx/templates/web_manifest.gotmpl diff --git a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/nginx/run b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/nginx/run index 677126a6d..e34ad3b1c 100755 --- a/docker/main/rootfs/etc/s6-overlay/s6-rc.d/nginx/run +++ b/docker/main/rootfs/etc/s6-overlay/s6-rc.d/nginx/run @@ -79,6 +79,16 @@ if [ ! \( -f "$letsencrypt_path/privkey.pem" -a -f "$letsencrypt_path/fullchain. -keyout "$letsencrypt_path/privkey.pem" -out "$letsencrypt_path/fullchain.pem" 2>/dev/null fi +# build templates for optional BASE_PATH environment variable +python3 /usr/local/nginx/get_base_path.py | \ + tempio -template /usr/local/nginx/templates/base_path.gotmpl \ + -out /usr/local/nginx/conf/base_path.conf + +# build templates for optional base_path in web manifest +python3 /usr/local/nginx/get_base_path.py | \ + tempio -template /usr/local/nginx/templates/web_manifest.gotmpl \ + -out /usr/local/nginx/conf/web_manifest.conf + # build templates for optional TLS support python3 /usr/local/nginx/get_tls_settings.py | \ tempio -template /usr/local/nginx/templates/listen.gotmpl \ diff --git a/docker/main/rootfs/usr/local/nginx/conf/nginx.conf b/docker/main/rootfs/usr/local/nginx/conf/nginx.conf index 8a98da1f2..1306d3610 100644 --- a/docker/main/rootfs/usr/local/nginx/conf/nginx.conf +++ b/docker/main/rootfs/usr/local/nginx/conf/nginx.conf @@ -96,6 +96,7 @@ http { gzip_types application/vnd.apple.mpegurl; include auth_location.conf; + include base_path.conf; location /vod/ { include auth_request.conf; @@ -235,9 +236,9 @@ http { include proxy.conf; } - location ~* /api/.*\.(jpg|jpeg|png|webp|gif)$ { + location ~* (?:/.*/)?api/.*\.(jpg|jpeg|png|webp|gif)$ { include auth_request.conf; - rewrite ^/api/(.*)$ /$1 break; + rewrite (?:/.*/)?api/(.*)$ /$1 break; proxy_pass http://frigate_api; include proxy.conf; } @@ -299,6 +300,8 @@ http { add_header Cache-Control "public"; } + include web_manifest.conf; + sub_filter 'href="/BASE_PATH/' 'href="$http_x_ingress_path/'; sub_filter 'url(/BASE_PATH/' 'url($http_x_ingress_path/'; sub_filter '"/BASE_PATH/dist/' '"$http_x_ingress_path/dist/'; diff --git a/docker/main/rootfs/usr/local/nginx/get_base_path.py b/docker/main/rootfs/usr/local/nginx/get_base_path.py new file mode 100644 index 000000000..c2a0987a5 --- /dev/null +++ b/docker/main/rootfs/usr/local/nginx/get_base_path.py @@ -0,0 +1,10 @@ +"""Prints the base path as json to stdout.""" + +import json +import os + +base_path = os.environ.get("BASE_PATH", "") + +result: dict[str, any] = {"base_path": base_path} + +print(json.dumps(result)) \ No newline at end of file diff --git a/docker/main/rootfs/usr/local/nginx/templates/base_path.gotmpl b/docker/main/rootfs/usr/local/nginx/templates/base_path.gotmpl new file mode 100644 index 000000000..b018b7af2 --- /dev/null +++ b/docker/main/rootfs/usr/local/nginx/templates/base_path.gotmpl @@ -0,0 +1,17 @@ +{{ if .base_path }} +location = {{ .base_path }} { + return 302 {{ .base_path }}/; +} + +location {{ .base_path }}/ { + proxy_http_version 1.1; + proxy_cache_bypass $http_upgrade; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $http_connection; + proxy_set_header Host $http_host; + proxy_set_header X-Ingress-Path {{ .base_path }}; + proxy_set_header Authorization ""; + proxy_pass http:/{{ .base_path }}:5000/; + access_log off; +} +{{ end }} \ No newline at end of file diff --git a/docker/main/rootfs/usr/local/nginx/templates/web_manifest.gotmpl b/docker/main/rootfs/usr/local/nginx/templates/web_manifest.gotmpl new file mode 100644 index 000000000..01892fbd6 --- /dev/null +++ b/docker/main/rootfs/usr/local/nginx/templates/web_manifest.gotmpl @@ -0,0 +1,13 @@ +{{ if .base_path }} +location ~ ^/.*-([A-Za-z0-9]+)\.webmanifest$ { + access_log off; + expires 1y; + add_header Cache-Control "public"; + default_type application/json; + proxy_set_header Accept-Encoding ""; + sub_filter_once off; + sub_filter_types application/json; + sub_filter '"start_url": "/"' '"start_url" : "{{ .base_path }}"'; + sub_filter '"src": "/' '"src": "{{ .base_path }}/'; +} +{{ end }} \ No newline at end of file