调试 Nginx 缓存未命中:尽管代理有效,但仍出现大量未命中

Qui*_*Par 7 nginx cache reverse-proxy

我的代理缓存路径设置为非常高的大小

proxy_cache_path  /var/lib/nginx/cache  levels=1:2   keys_zone=staticfilecache:180m  max_size=700m;
Run Code Online (Sandbox Code Playgroud)

并且使用的大小仅为

sudo du -sh *
14M cache
4.0K    proxy
Run Code Online (Sandbox Code Playgroud)

代理缓存有效设置为

proxy_cache_valid 200 120d;
Run Code Online (Sandbox Code Playgroud)

我通过以下方式跟踪 HIT 和 MISS

add_header X-Cache-Status $upstream_cache_status;
Run Code Online (Sandbox Code Playgroud)

尽管有这些设置,我还是看到了很多未命中。这是我在一小时前故意运行缓存预热器的页面。

我如何调试这些 MISS 发生的原因?我如何确定未命中是否是由于驱逐、到期、某些流氓头球等造成的?Nginx 是否为此提供命令?

编辑:完整配置

    # at http level
    proxy_cache_path  /var/lib/nginx/cache  levels=1:2 inactive=400d keys_zone=staticfilecache:180m  max_size=700m;
    proxy_temp_path /var/lib/nginx/proxy;
    proxy_connect_timeout 30;
    proxy_read_timeout 120;
    proxy_send_timeout 120;
    #prevent header too large errors
    proxy_buffers 256 16k;
    proxy_buffer_size 32k;
    #httpoxy exploit protection
    proxy_set_header Proxy "";

    # at server level 
    add_header Cache-BYPASS-Reason $skip_reason;

    # define nginx variables
    set $do_not_cache 0;
    set $skip_reason "";
    set $bypass 0;

    # security for bypass so localhost can empty cache
    if ($remote_addr ~ "^(127.0.0.1|Web.Server.IP)$") {
        set $bypass $http_8X0;
    }

    # skip caching WordPress cookies
    if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
        set $do_not_cache 1;
        set $skip_reason Cookie;
    }

    # Don't cache URIs containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php") {
        set $skip_cache 1;
        set $skip_reason URI;
    }

    # https://guides.wp-bullet.com/how-to-configure-nginx-reverse-proxy-wordpress-cache-apache/
    location / {
            proxy_pass http://127.0.0.1:8000;

            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 https;
            proxy_set_header X-Forwarded-Port 443;
            proxy_set_header Host $host;
            proxy_set_header Accept-Encoding "";

            # may need to comment out proxy_redirect if get login redirect loop
            proxy_redirect off;

            proxy_cache_key "$scheme://$host$uri";
            add_header X-Nginx-Cache-Head "$scheme://$host$uri";
            proxy_cache staticfilecache;
            proxy_cache_valid       200 301 302 100d;
            proxy_cache_valid 404 1m;


            add_header Cache-Control public;

            proxy_ignore_headers Expires;
            proxy_ignore_headers  "Cache-Control";
            proxy_ignore_headers X-Accel-Expires;

            proxy_hide_header "Cache-Control";
            proxy_hide_header Pragma;
            proxy_hide_header Server;
            proxy_hide_header Request-Context;
            proxy_hide_header X-Powered-By;
            proxy_cache_revalidate on;

            proxy_hide_header X-AspNet-Version;
            proxy_hide_header X-AspNetMvc-Version;
            #proxy_pass_header X-Accel-Expires;


            add_header X-Nginx-Cache-Status $upstream_cache_status;

            proxy_cache_use_stale  error timeout invalid_header updating http_500 http_502 http_503 http_504;
            proxy_cache_bypass $arg_nocache $do_not_cache $http_8X0;
            proxy_no_cache $do_not_cache;

    }

    location ~* \.(jpg|png|gif|jpeg|css|js|mp3|wav|swf|mov|doc|pdf|xls|ppt|docx|pptx|xlsx)$ {
            proxy_cache_valid 200 120d;
            expires 364d;
            add_header Cache-Control public;
            proxy_pass http://127.0.0.1:8000;
            proxy_cache staticfilecache;
            add_header X-Nginx-Cache-Status $upstream_cache_status;
            proxy_cache_use_stale  error timeout invalid_header updating http_500 http_502 http_503 http_504;
    }
Run Code Online (Sandbox Code Playgroud)

Leo*_*Leo 6

缓存:

你是proxy_cache在你的location还是server块中启用?

例如,location /来自Nginx 文档的块中的一些设置。

proxy_cache_path  /var/lib/nginx/cache  levels=1:2   keys_zone=staticfilecache:180m  max_size=700m;


server {
    # ...
    location / {
        proxy_cache my_cache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating http_500 http_502
                              http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;
    # ...
    }
Run Code Online (Sandbox Code Playgroud)

要使缓存正常工作,您至少需要两个强制性设置:

如果您将它设置在某个location块中,您确定这是您想要缓存的块吗?


分析

如果您想分析命中,您可以为此创建一个特定的日志:

log_format cache_st '$remote_addr - $upstream_cache_status [$time_local]  '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';
Run Code Online (Sandbox Code Playgroud)

并且在同一个serverlocation块中,您可以将其添加为辅助日志,这样您就不会错过其他内容:

access_log   /var/log/nginx/domain.com.access.log;
access_log   /var/log/nginx/domain.com.cache.log cache_st;
Run Code Online (Sandbox Code Playgroud)

然后你可以检查一些统计数据:

命中 vs 未命中 vs 绕过 vs 过期

awk '{print $3}' cache.log | sort | uniq -c | sort -r

错过网址

awk '($3 ~ /MISS/)' cache.log | awk '{print $7}' | sort | uniq -c | sort -r

绕过网址

awk '($3 ~ /BYPASS/)' cache.log | awk '{print $7}' | sort | uniq -c | sort -r

错过与绕过

  • 当模式配置为缓存但在请求时未缓存时,会发生MISS。在正确的配置中,后续请求将根据缓存持续时间其他参数从缓存中提供。
  • 当模式被明确配置为不使用缓存时,就会发生BYPASS。例如,跳过登录用户的缓存。后续请求也将被绕过。

分析来源: - https://easyengine.io/tutorials/nginx/upstream-cache-status-in-access-log/

通过控制台即时分析的另一种选择是使用 GoAccess,这是一个非常好的实时网络日志分析器,只需要ncurses即可工作:https : //goaccess.io/


Mik*_*den 4

您可能需要将该inactive参数设置proxy_cache_path为大于120d(或您希望的最大缓存时间实际值)。不活动的默认设置是 10 分钟。只要您正在缓存的 URL 在非活动参数的时间范围内被访问,您的缓存就是有效的,但如果在该时间范围内没有访问它,它将从缓存中消失。有关更多信息,请参阅了解 nginx proxy_cache_path 指令。

我相信这不属于典型的 $upstream_cache_status 风格调试,因为缓存清理不会在请求/响应周期内发生。据我所知,如果 nginx 工作进程不做任何其他事情,它会将缓存清理作为低优先级任务。我不确定此活动会显示在日志中的何处,但它可能只会在启用调试的构建中显示。