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

前端之家收集整理的这篇文章主要介绍了调试Nginx缓存未命中:尽管代理有效,但仍会遇到大量的MISS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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;
    }
最佳答案
您可能需要将proxy_cache_path上的inactive参数设置为大于120d的值(或者您希望实际存在的最大缓存时间). default setting for inactive is 10 minutes.只要在非活动参数的时间范围内访问您正在缓存的URL,您的缓存就是有效的,但如果在该时间范围内没有访问它,它将从缓存中删除.有关更多信息,请参见Understanding the nginx proxy_cache_path directive.

我认为这不属于typical $upstream_cache_status style debugging,因为缓存清理不会在请求/响应周期内发生. AFAIK是一个Nginx工作进程,如果它没有做任何其他事情,它会将清理缓存为低优先级任务.我不确定这个活动会在日志中显示的位置,但它可能只会显示启用调试的构建.

原文链接:https://www.f2er.com/nginx/435172.html

猜你在找的Nginx相关文章