forked from Viren070/docker-compose-template
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(aiometadata): add optional poster reverse-proxy cache #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| user nginx; | ||
| worker_processes auto; | ||
|
|
||
| events { | ||
| worker_connections 1024; | ||
| } | ||
|
|
||
| http { | ||
| # Cache storage on disk — adjust max_size to suit available space | ||
| proxy_cache_path /var/cache/nginx/posters | ||
| levels=1:2 | ||
| keys_zone=poster_cache:10m | ||
| max_size=10g | ||
| inactive=30d | ||
| use_temp_path=off; | ||
|
|
||
| # Restore double-slash after scheme when a reverse proxy (e.g. Traefik) | ||
| # collapses "https://" to "https:/". | ||
| # Input: /https:/api.example.com/path -> https://api.example.com/path | ||
| # Input: /https://api.example.com/path -> https://api.example.com/path | ||
| map $request_uri $upstream_url { | ||
| ~^/(https?):/([^/].*)$ $1://$2; | ||
| ~^/(https?://.*)$ $1; | ||
| default ""; | ||
| } | ||
|
IbbyLabs marked this conversation as resolved.
|
||
|
|
||
| # Extract scheme + host from the upstream URL for resolving relative redirects | ||
| map $upstream_url $upstream_origin { | ||
| ~^(https?://[^/]+) $1; | ||
| default ""; | ||
| } | ||
|
|
||
| log_format cache '$remote_addr - [$time_local] "$request" $status ' | ||
| '$body_bytes_sent $upstream_cache_status'; | ||
| access_log /var/log/nginx/access.log cache; | ||
|
|
||
| server { | ||
| listen 8888; | ||
|
|
||
| location = /health { | ||
| access_log off; | ||
| return 200 'ok'; | ||
| } | ||
|
|
||
| location = /stats { | ||
| access_log off; | ||
| default_type application/json; | ||
| alias /tmp/cache-stats.json; | ||
| } | ||
|
|
||
| location = /purge { | ||
| access_log off; | ||
| default_type application/json; | ||
| proxy_pass http://127.0.0.1:9888; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| location / { | ||
| resolver 127.0.0.11 valid=30s ipv6=off; | ||
|
|
||
| if ($upstream_url = "") { | ||
| return 400; | ||
| } | ||
|
|
||
| proxy_pass $upstream_url; | ||
| proxy_ssl_server_name on; | ||
|
|
||
| # Rewrite relative upstream redirects into absolute URLs. | ||
| # Some upstreams (e.g. openposterdb) return relative 302 Location headers | ||
| # like "/c/abc/path" which the client would resolve against the proxy host. | ||
| # This rewrites them to point to the actual upstream origin. | ||
| # e.g. Location: /c/abc/path → Location: https://openposterdb.com/c/abc/path | ||
| proxy_redirect / $upstream_origin/; | ||
|
|
||
| proxy_cache poster_cache; | ||
| proxy_cache_key $upstream_url; | ||
| proxy_cache_valid 200 30d; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| proxy_ignore_headers Cache-Control Expires Vary; | ||
| proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; | ||
| proxy_cache_lock on; | ||
|
|
||
| add_header X-Cache-Status $upstream_cache_status; | ||
|
|
||
| proxy_set_header Host $proxy_host; | ||
| proxy_set_header Accept-Encoding ""; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/bin/sh | ||
| # HTTP handler for /purge — called by nc -lk -e | ||
| read -r method path _ | ||
| # Consume remaining headers | ||
| while read -r line; do | ||
| line=$(printf '%s' "$line" | tr -d '\r\n') | ||
| [ -z "$line" ] && break | ||
| done | ||
|
|
||
| touch /tmp/purge-cache | ||
| BODY='{"success":true,"message":"cache purge scheduled"}' | ||
| printf "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s" ${#BODY} "$BODY" | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/bin/sh | ||
| # Periodically writes cache stats to a JSON file served by nginx | ||
| CACHE_DIR="/var/cache/nginx/posters" | ||
| STATS_FILE="/tmp/cache-stats.json" | ||
| MAX_SIZE="${POSTER_CACHE_MAX_SIZE:-10g}" | ||
| INACTIVE="${POSTER_CACHE_INACTIVE:-30d}" | ||
|
|
||
| while true; do | ||
| if [ -d "$CACHE_DIR" ]; then | ||
| size_bytes=$(du -sb "$CACHE_DIR" 2>/dev/null | cut -f1) | ||
| file_count=$(find "$CACHE_DIR" -type f 2>/dev/null | wc -l) | ||
| size_human=$(awk "BEGIN { | ||
| b = ${size_bytes:-0}; | ||
| if (b >= 1000000000) printf \"%.1fG\", b/1000000000; | ||
| else if (b >= 1000000) printf \"%.1fM\", b/1000000; | ||
| else if (b >= 1000) printf \"%.1fK\", b/1000; | ||
| else printf \"%dB\", b; | ||
| }") | ||
| else | ||
| size_bytes=0 | ||
| size_human="0B" | ||
| file_count=0 | ||
| fi | ||
|
|
||
| # Check for purge flag | ||
| if [ -f /tmp/purge-cache ]; then | ||
| rm -f /tmp/purge-cache | ||
| rm -rf "$CACHE_DIR" | ||
| mkdir -p "$CACHE_DIR" | ||
| chown nginx:nginx "$CACHE_DIR" | ||
| size_bytes=0 | ||
| size_human="0B" | ||
| file_count=0 | ||
| fi | ||
|
|
||
| cat > "$STATS_FILE" <<EOF | ||
| {"cached_images":${file_count},"disk_usage":"${size_human}","disk_usage_bytes":${size_bytes},"max_size":"${MAX_SIZE}","inactive":"${INACTIVE}"} | ||
| EOF | ||
|
IbbyLabs marked this conversation as resolved.
|
||
| sleep 30 | ||
| done | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.