AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 911921
Accepted
Quintin Par
Quintin Par
Asked: 2018-05-13 07:59:33 +0800 CST2018-05-13 07:59:33 +0800 CST 2018-05-13 07:59:33 +0800 CST

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

  • 772

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

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

并且使用的尺寸只有

sudo du -sh *
14M cache
4.0K    proxy

代理缓存有效设置为

proxy_cache_valid 200 120d;

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

add_header X-Cache-Status $upstream_cache_status;

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

我如何调试为什么会发生这些 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;
    }
nginx
  • 3 3 个回答
  • 15910 Views

3 个回答

  • Voted
  1. Leo
    2018-05-19T17:37:27+08:002018-05-19T17:37:27+08:00

    缓存:

    你proxy_cache在你的location或server块中启用了吗?

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

    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;
        # ...
        }
    

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

    • proxy_cache_path
    • 代理缓存

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


    分析

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

    log_format cache_st '$remote_addr - $upstream_cache_status [$time_local]  '
                        '"$request" $status $body_bytes_sent '
                        '"$http_referer" "$http_user_agent"';
    

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

    access_log   /var/log/nginx/domain.com.access.log;
    access_log   /var/log/nginx/domain.com.cache.log cache_st;
    

    然后,您可以检查一些统计信息:

    HIT vs MISS vs BYPASS vs EXPIRED

    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,这是一个非常好的实时 Web 日志分析器,只需要ncurses即可工作:https ://goaccess.io/

    • 6
  2. Best Answer
    Mike Howsden
    2018-05-22T10:19:05+08:002018-05-22T10:19:05+08:00

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

    我相信这不属于典型的 $upstream_cache_status 样式调试,因为缓存清理不会在请求/响应周期内发生。AFAIK 一个 nginx 工作进程如果不做任何其他事情,它会将缓存清理为低优先级任务。我不确定此活动会出现在日志中的哪个位置,但它可能只会出现在启用调试的构建中。

    • 3
  3. x86fantini
    2018-05-22T10:50:20+08:002018-05-22T10:50:20+08:00

    试图缓存什么?厘米?静态页面?通常,如果 backed send no-cache 、 expire -1 或 cache private ,你会得到 misses 。如果是 cookie,您也会失败。

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve