|
| 1 | +apiVersion: v1 |
| 2 | +kind: ConfigMap |
| 3 | +metadata: |
| 4 | + name: nginx-config |
| 5 | + namespace: cdn |
| 6 | +data: |
| 7 | + nginx.conf: | |
| 8 | + worker_processes auto; |
| 9 | + error_log /var/log/nginx/error.log warn; |
| 10 | + pid /var/cache/nginx/nginx.pid; |
| 11 | +
|
| 12 | + # Tune for large file serving |
| 13 | + worker_rlimit_nofile 65535; |
| 14 | +
|
| 15 | + events { |
| 16 | + worker_connections 4096; |
| 17 | + use epoll; |
| 18 | + multi_accept on; |
| 19 | + } |
| 20 | +
|
| 21 | + http { |
| 22 | + include /etc/nginx/mime.types; |
| 23 | + default_type application/octet-stream; |
| 24 | +
|
| 25 | + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
| 26 | + '$status $body_bytes_sent "$http_referer" ' |
| 27 | + '"$http_user_agent" "$http_x_forwarded_for" ' |
| 28 | + 'cache=$upstream_cache_status'; |
| 29 | +
|
| 30 | + access_log /var/log/nginx/access.log main; |
| 31 | +
|
| 32 | + # Large file optimisations |
| 33 | + sendfile on; |
| 34 | + tcp_nopush on; |
| 35 | + tcp_nodelay on; |
| 36 | + keepalive_timeout 65; |
| 37 | +
|
| 38 | + # Proxy cache zone configuration: |
| 39 | + # keys_zone=cdn_cache:50m — 50MB for cache keys/metadata (~400k keys) |
| 40 | + # max_size=50g — on-disk cache (adjust to your PVC size) |
| 41 | + # inactive=30d — evict if not accessed in this time |
| 42 | + # use_temp_path=off — write directly to cache dir (avoids extra copy) |
| 43 | + proxy_cache_path /var/cache/nginx/cdn |
| 44 | + levels=1:2 |
| 45 | + keys_zone=cdn_cache:50m |
| 46 | + max_size=5g |
| 47 | + inactive=7d |
| 48 | + use_temp_path=off; |
| 49 | +
|
| 50 | + # Don't buffer large files to disk before sending — stream them |
| 51 | + proxy_buffering on; |
| 52 | + proxy_request_buffering off; |
| 53 | +
|
| 54 | + # Increase timeouts for large file transfers |
| 55 | + proxy_connect_timeout 10s; |
| 56 | + proxy_send_timeout 300s; |
| 57 | + proxy_read_timeout 300s; |
| 58 | + send_timeout 300s; |
| 59 | +
|
| 60 | + # Hide upstream headers we don't want to leak |
| 61 | + proxy_hide_header x-amz-request-id; |
| 62 | + proxy_hide_header x-amz-id-2; |
| 63 | +
|
| 64 | + include /etc/nginx/conf.d/*.conf; |
| 65 | + } |
| 66 | + default.conf: | |
| 67 | + # Upstream: your S3 S3 origin |
| 68 | + # In production this points at your S3 service endpoint. |
| 69 | + # Can also point at R2/S3 by changing the server and Host header. |
| 70 | + upstream s3_origin { |
| 71 | + server rook-ceph-rgw-ceph-objectstore.rook-ceph.svc:80; |
| 72 | + keepalive 32; |
| 73 | + } |
| 74 | +
|
| 75 | + server { |
| 76 | + listen 8080; |
| 77 | + server_name _; |
| 78 | +
|
| 79 | + # TLS — cert mounted from a k8s secret via ingress or directly |
| 80 | + #ssl_certificate /etc/nginx/tls/tls.crt; |
| 81 | + #ssl_certificate_key /etc/nginx/tls/tls.key; |
| 82 | + #ssl_protocols TLSv1.2 TLSv1.3; |
| 83 | + #ssl_ciphers HIGH:!aNULL:!MD5; |
| 84 | +
|
| 85 | + proxy_cache cdn_cache; |
| 86 | + proxy_cache_valid 200 206 7d; # Cache 200 and partial content |
| 87 | + proxy_cache_valid 404 1m; # Don't cache 404s for long |
| 88 | + proxy_cache_use_stale error timeout updating http_500 http_502 http_503; |
| 89 | + proxy_cache_lock on; # Collapse simultaneous requests for the same file |
| 90 | + proxy_cache_lock_timeout 10s; |
| 91 | +
|
| 92 | + proxy_cache_key "$scheme$proxy_host$uri"; |
| 93 | +
|
| 94 | + add_header X-Cache-Status $upstream_cache_status always; |
| 95 | + add_header X-Served-By $hostname always; |
| 96 | +
|
| 97 | + add_header X-Content-Type-Options nosniff always; |
| 98 | + add_header X-Frame-Options DENY always; |
| 99 | +
|
| 100 | + proxy_cache_revalidate on; |
| 101 | + proxy_cache_bypass 0; |
| 102 | + proxy_no_cache 0; |
| 103 | + proxy_ignore_headers Cache-Control Expires Set-Cookie; |
| 104 | +
|
| 105 | + location / { |
| 106 | + # Forward to Object Storage. |
| 107 | + # S3-compatible API expects requests in the form: /bucket/key |
| 108 | + proxy_pass http://s3_origin/firmware-images$request_uri; |
| 109 | +
|
| 110 | + proxy_http_version 1.1; |
| 111 | + proxy_set_header Connection ""; # keepalive to upstream |
| 112 | + proxy_set_header Host rook-ceph-rgw-ceph-objectstore.rook-ceph.svc; |
| 113 | + proxy_set_header X-Real-IP $remote_addr; |
| 114 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 115 | +
|
| 116 | + # Don't forward auth headers downstream |
| 117 | + proxy_set_header Authorization ""; |
| 118 | +
|
| 119 | + # Tell clients files are immutable — they should cache forever |
| 120 | + add_header Cache-Control "public, max-age=31536000, immutable" always; |
| 121 | +
|
| 122 | + # Support resumable downloads |
| 123 | + proxy_force_ranges on; |
| 124 | +
|
| 125 | + # Stream large files rather than buffering defeats the cache, so keep buffering on: |
| 126 | + proxy_buffering on; |
| 127 | + } |
| 128 | +
|
| 129 | + # Health check endpoint (used by k8s liveness/readiness probes) |
| 130 | + location /healthz { |
| 131 | + access_log off; |
| 132 | + return 200 "ok\n"; |
| 133 | + add_header Content-Type text/plain; |
| 134 | + } |
| 135 | +
|
| 136 | + # Expose basic cache stats (restrict to internal) |
| 137 | + location /nginx_status { |
| 138 | + stub_status; |
| 139 | + allow 10.0.0.0/8; |
| 140 | + allow 172.16.0.0/12; |
| 141 | + allow 192.168.0.0/16; |
| 142 | + deny all; |
| 143 | + } |
| 144 | + } |
0 commit comments