у нас есть статический кэш nginx, в котором каждый узел в кластере действует как балансировщик нагрузки и узлы данных.
Мы используем согласованное хеширование для балансировки нагрузки.Вот простая настройка
HTTP-клиент -> Балансировщик нагрузки -> [вернуть, если результат доступен в кеше] -> маршрутизация путем использования-согласованного хэширования -> [вернуть, если кэшировано] -> [прочитатьиз s3]
Путь кеша прокси:
proxy_cache_path /var/cache/lb-nginx
levels=1:2 keys_zone=lb_cache:10m max_size=200g
loader_files=10000 loader_threshold=10m loader_sleep=50ms
inactive=365d use_temp_path=off;
proxy_cache_path /var/cache/nginx
levels=1:2 keys_zone=s3_cache:10m max_size=600g
loader_files=10000 loader_threshold=10m loader_sleep=50ms
inactive=365d use_temp_path=off;
Конфигурация восходящего потока
# load balancer redirects autogen
upstream backend {
hash $request_uri consistent;
# This helps to not to create connection to backend everytime.
keepalive 34 ;
}
# load balancer end point
server {
error_log /var/log/nginx/xxx_debug_lb.log debug;
listen 80;
server_name hostname.abc.yyz 1.2.3.4; # hostname ipaddress
location / {
resolver 1.2.3.4;
# refer : http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
# Proxy backend settings.
proxy_intercept_errors on;
proxy_pass http://backend;
error_log /var/log/nginx/_debug_lb_2.log debug;
# Cache results in LB as well to save bandwidth
proxy_cache lb_cache;
proxy_cache_valid 200 30d; # 30 days
proxy_cache_valid 404 1d; # 1 day
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
proxy_cache_bypass $http_cache_purge;
# disable convert get to head, otherwise, HEAD request will be denied
proxy_cache_convert_head off;
# disable cache convert requires setting cache key, $request_method is not part of proxy_cache_key by default
proxy_cache_methods GET HEAD;
proxy_cache_key $scheme$host$request_method$request_uri;
add_header Cache-Control max-age=2851000;
add_header X-Lb-Cache-Status $upstream_cache_status;
# http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
# make sure the response will be cached.
proxy_ignore_headers Set-Cookie;
}
}
Конечная точка S3
server {
error_log /var/log/nginx/_debug_s3.log debug;
listen 80 default_server;
location /lua_debug {
return 200 "Hello world!";
}
location / {
# Calling aws_auth, to set HTTP Headers
set $s3_host s3.amazonaws.com;
set $bucket "abcd"
set $s3_uri "/$bucket$uri";
access_by_lua "local aws = require 'resty.aws_auth'; aws.s3_set_headers(ngx.var.s3_host, ngx.var.s3_uri)";
# Somehow nginx is looking for resolver ¯\_(ツ)_/¯
resolver 1.2.3.4;
# refer : http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors
# Proxy backend settings.
proxy_intercept_errors on;
proxy_pass https://$s3_host$s3_uri;
error_log /var/log/nginx/_debug_s3_2.log debug;
# Cache related settings
proxy_cache s3_cache;
proxy_cache_valid 200 365d; # 1year
proxy_cache_valid 404 1d; # 1day
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
proxy_cache_bypass $http_cache_purge;
# disable convert get to head, otherwise, HEAD request will be denied
proxy_cache_convert_head off;
# disable cache convert requires setting cache key, $request_method is not part of proxy_cache_key by default
proxy_cache_methods GET HEAD;
proxy_cache_key $scheme$request_method$proxy_host$request_uri;
add_header Cache-Control max-age=31536000;
add_header X-Cache-Status $upstream_cache_status;
# http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
# make sure the response will be cached.
proxy_ignore_headers Set-Cookie;
}
}
Однако, когда я пытаюсьнажмите на балансировщик нагрузки, используя IP-адрес (поскольку имя_сервера поддерживает как ip, так и имя хоста), 1. для начального запроса он показывает кеш-HIT / MISS
X-Cache-Status: HIT X-Lb-Cache-Статус: MISS
для последующего запроса HIT / HIT
X-Cache-Status: HIT X-Lb-Cache-Status: HIT
Примерно через ~15 минут снова возникает ошибка кеша
X-Cache-Status: HIT X-Lb-Cache-Status: MISS
Может кто-нибудь мне помочь, кто яотсутствует