Nginx 缓存,未命中时重定向/绕过

Nginx 缓存,未命中时重定向/绕过

是否可以设置 nginx 缓存(nginx_proxy_cache)以不同方式处理 MISS/BYPASS:

  1. 缓存存在 -> 使用它
  2. 缓存 MISSed/BYPASSed/UPDATing -> 重定向到后端服务器。

行为示例

我为什么需要这个?

我有 6 台服务器,其中静态 mp4 文件(全部为克隆文件)存储在 SATA 驱动器上。我又买了一台带 SSD 的新服务器来测试性能。

如果我使用标准 nginx 缓存,是的,它会工作......几个小时后它会从超载的服务器下载 1k x 10MB 文件:)

我正在尝试实现:“如果服务器已缓存,则在缓存/更新时重定向到后端(而不是 BYPASS!)”

正如评论中所问,如果我访问不在缓存中的 URL,我会得到这样的响应:

GET http://xxx/content/mp4/yyy/zzz.mp4

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 16 Dec 2016 23:44:54 GMT
Content-Type: video/mp4
Content-Length: 25065219
Connection: keep-alive
Last-Modified: Mon, 22 Jun 2015 02:34:00 GMT
ETag: "55877418-17e7703"
X-Cache-Status: MISS
Accept-Ranges: bytes

如您所见,其中有我想要的“位置”字段。并且 nginx 当前将视频文件通过自身“传递”到浏览器以及缓存。如果我在“更新”期间再发出 1 个请求,它将再次传递。

我真正想要得到的(不涉及 php/ruby 等)是这样的响应:

GET http://xxx/content/mp4/yyy/zzz.mp4

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 16 Dec 2016 23:44:54 GMT
Content-Type: video/mp4
Content-Length: 25065219
Connection: keep-alive
Last-Modified: Mon, 22 Jun 2015 02:34:00 GMT
ETag: "55877418-17e7703"
X-Cache-Status: MISS
Accept-Ranges: bytes
Location: http://backend/content/mp4/yyy/zzz.mp4

同时让 nginx 将此文件填充到缓存中。

或者也许在 Varnish 中实现此行为会更简单?还没有尝试。

nginx.conf

s001:/etc/nginx# cat nginx.conf

user XXX;
pid /run/nginx.pid;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
worker_rlimit_nofile 30000;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 2048;
    multi_accept on;
}

http {
    server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format ups '$remote_addr $host [$time_local] "$request" "$upstream_response_time"';
    log_format IP '[$time_local] $http_referer $request $remote_addr';
    log_format vhost '$remote_addr $host $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$request_body" "$http_user_agent" upstrea_response "$upstream_response_time"';

    access_log /var/log/nginx/access.log vhost;

    sendfile on;
    sendfile_max_chunk 1m;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65s;

    client_body_buffer_size 8k;
    client_max_body_size 80M;
    client_body_timeout 60s;
    large_client_header_buffers 4 16k;
    send_timeout 60s;
    types_hash_max_size 2048;

    gzip off;
    gzip_static off;
    gzip_proxied expired no-cache no-store private auth;
    gzip_vary on;
    gzip_min_length 1100;
    gzip_buffers 64 8k;
    gzip_comp_level 2;
    gzip_http_version 1.1;
    gzip_proxied any;
    gzip_types text/plain application/xml application/x-javascript text/css video/x-flv application/octet-stream video/mpeg video/mp4;
    server_names_hash_bucket_size 64;

    proxy_connect_timeout 60s;
    proxy_read_timeout 60s;
    proxy_buffering on;
    proxy_force_ranges on;
    proxy_http_version 1.1;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    proxy_pass_request_headers on;

    upstream uHDDs {
        server 172.16.0.5;
        server 172.16.0.6;
        server 172.16.0.7;
        server 172.16.0.8;
        server 172.16.0.9;
        server 172.16.0.10;
    }

    add_header X-Cache-Status $upstream_cache_status;

    proxy_cache_path /mnt/ssd2/nginx_cache levels=1:2 keys_zone=ssd2_nginx_cache:256m max_size=400g inactive=72h use_temp_path=off;

    # proxy_cache_key $scheme$proxy_host$uri;

    proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
    proxy_cache_valid any 24h;
    proxy_cache_min_uses 1;

    proxy_cache_lock on;
    proxy_cache_lock_age 180s;
    proxy_cache_lock_timeout 100ms;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

站点启用/0_default.conf

server {
    listen 80 default;
    server_name XXX;
    charset utf8;
    root /home/XXX/www/s1;

    proxy_bind 172.16.1.1;
    proxy_cache_key $uri;
    proxy_cache_bypass $cookie_nocache $arg_nocache;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    location ^~ /v/ {
        rewrite ^/v/(\w+)/(.+)$ /content/$2 break;

        proxy_cache ssd2_nginx_cache;
        proxy_pass http://172.16.0.5$uri?$args;
    }

    location ^~ /content/ {
        proxy_cache ssd2_nginx_cache;
        proxy_pass http://172.16.0.5$uri?$args;
    }

    location /nginx_status {
        stub_status on;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

答案1

Nginx 解决方案:未找到。

清漆:可以使用通过/未通过处理程序来完成。

答案2

如果 http 重定向 301/302 是必需的(而不是代理、提供响应主体加上缓存),我建议重定向所有请求并通过以下方式缓存它们proxy_cache_valid

proxy_cache_valid 301      24h;

相关内容