feat: Implementa Cache Inteligente com SWR e Invalidação dinâmica de Assets
This commit is contained in:
parent
5d1936e63e
commit
859d0851e5
|
|
@ -72,10 +72,10 @@ server {
|
||||||
|
|
||||||
# 2. Assets Estáticos (CACHE AGRESSIVO & MODERN)
|
# 2. Assets Estáticos (CACHE AGRESSIVO & MODERN)
|
||||||
location ~* \.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg|mjs|js|ts|wasm|json|woff2?|ttf|otf|eot|pdf|css|less|scss)$ {
|
location ~* \.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg|mjs|js|ts|wasm|json|woff2?|ttf|otf|eot|pdf|css|less|scss)$ {
|
||||||
expires $cache_asset_ttl;
|
include snippets/cache_optimizer.conf;
|
||||||
add_header Cache-Control "public, immutable";
|
add_header Cache-Control $cache_control_header;
|
||||||
add_header Alt-Svc 'h3=":443"; ma=86400';
|
|
||||||
proxy_cache_valid 200 30d;
|
proxy_cache_valid 200 $cache_asset_ttl;
|
||||||
proxy_pass http://ferreirareal_backend;
|
proxy_pass http://ferreirareal_backend;
|
||||||
|
|
||||||
# Rate Limit Diferenciado
|
# Rate Limit Diferenciado
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
# --- Pathfinder Smart Cache Optimization ---
|
||||||
|
|
||||||
|
# 1. Stale-While-Revalidate (SWR) Global
|
||||||
|
# Serve conteúdo antigo enquanto atualiza em background (Ultra rápido)
|
||||||
|
proxy_cache_revalidate on;
|
||||||
|
proxy_cache_background_update on;
|
||||||
|
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
|
||||||
|
|
||||||
|
# 2. Configurações de Cache-Control por Tipo de Arquivo
|
||||||
|
# Nota: $cache_asset_ttl vem do security_maps.conf
|
||||||
|
add_header X-Cache-Status $upstream_cache_status;
|
||||||
|
|
||||||
|
# Trata HTML: Cachear mas sempre perguntar ao servidor se mudou (no-cache)
|
||||||
|
# Isso permite o 304 Not Modified, evitando download desnecessário mas garantindo atualização.
|
||||||
|
map $sent_http_content_type $cache_control_header {
|
||||||
|
default "no-cache, no-store, must-revalidate";
|
||||||
|
~*text/html "no-cache, must-revalidate";
|
||||||
|
~*application/json "no-cache, must-revalidate";
|
||||||
|
~*(image|audio|video|font) "public, max-age=31536000, immutable";
|
||||||
|
~*(text/css|javascript) $asset_cache_policy;
|
||||||
|
}
|
||||||
|
|
||||||
|
# 3. Política Diferenciada para JS/CSS
|
||||||
|
# Se tiver versão (?v= ou .v1.) -> Cache Eterno
|
||||||
|
# Se for nome limpo -> Cache curto com revalidação obrigatória
|
||||||
|
map $request_uri $asset_cache_policy {
|
||||||
|
~*(\?v=|\?id=|\.v[0-9]|\.[0-9a-f]{8,}) "public, max-age=31536000, immutable";
|
||||||
|
default "public, max-age=3600, must-revalidate";
|
||||||
|
}
|
||||||
|
|
@ -114,15 +114,14 @@ map $security_score $heavy_limit_key {
|
||||||
}
|
}
|
||||||
|
|
||||||
# 3. Cache Asset TTL - Suporte Total 2026 (Modern Web)
|
# 3. Cache Asset TTL - Suporte Total 2026 (Modern Web)
|
||||||
|
# No proxy_cache usamos um tempo curto, o Cache-Control (Browser) é que decide o tempo longo.
|
||||||
map $request_uri $cache_asset_ttl {
|
map $request_uri $cache_asset_ttl {
|
||||||
# Imagens Modernas e Tradicionais
|
# Imagens e Mídia
|
||||||
~*\.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg)$ 1y;
|
~*\.(webp|avif|heic|apng|jpg|jpeg|gif|png|ico|svg)$ 1d;
|
||||||
# Scripts Modernos (Modules) e Tradicionais
|
# Scripts e Estilos (Revalidação rápida no Proxy)
|
||||||
~*\.(mjs|js|ts|wasm|json)$ 1y;
|
~*\.(mjs|js|ts|wasm|json|css|less|scss)$ 6h;
|
||||||
# Fontes e Documentos
|
# Fontes e Documentos
|
||||||
~*\.(woff2?|ttf|otf|eot|pdf)$ 1y;
|
~*\.(woff2?|ttf|otf|eot|pdf)$ 7d;
|
||||||
# Estilos e Outros
|
# Padrão: Sem Cache
|
||||||
~*\.(css|less|scss)$ 1y;
|
|
||||||
# Padrão: Sem Cache de TTL Longo
|
|
||||||
default off;
|
default off;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue