首页 > 解决方案 > 为什么nginx不缓存?

问题描述

我正在使用以下 nginx 配置来缓存 s3 响应,我能够访问 s3,但无法缓存任何内容。不知道为什么!

proxy_cache_path /var/cache/nginx
                   levels=1:2 keys_zone=s3_cache:1024m max_size=250g
                   loader_files=150000 loader_threshold=1s loader_sleep=50ms
                   inactive=365d use_temp_path=off;

    location / {
        # Calling aws_auth, to set HTTP Headers
        set $s3_host s3.amazonaws.com;
        set $s3_uri "/$arg_bucket$uri";
        access_by_lua "local aws = require 'resty.aws_auth'; aws.s3_set_headers(ngx.var.s3_host, ngx.var.s3_uri)";

        resolver               ...;


        # Cache related settings
        proxy_cache            s3_cache;
        proxy_cache_valid      200 30d; # 1 month
        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$s3_uri;
        # This max-age is different from "inactive" in proxy_cache_path.
        # It tells the browser or intermediary cache how long the response can be used from the time it was requested.
        add_header             Cache-Control max-age=2592000;
        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 Vary;

        proxy_pass             https://$s3_host$s3_uri;
    }
}

error/access.log 没有说系统有任何问题。不确定我是否遗漏了什么。

标签: nginxnginx-reverse-proxynginx-config

解决方案


http级别有proxy_buffering off,删除该行后,它开始缓存。

不幸的是,没有errors.log关于此的错误或文档。


推荐阅读