nginx x-cache 状态在 WordPress 网站上的命中和过期之间切换(测试 php 文件正常)

nginx x-cache 状态在 WordPress 网站上的命中和过期之间切换(测试 php 文件正常)

x-cache: HIT我为 WordPress 网站设置了一个 nginx 缓存,它在 nginx 标头状态和看似随机的状态之间切换x-cache: EXPIRED

C:\Users\curl-7.48>curl -I http://example.com
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sun, 03 Apr 2016 14:15:15 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Cached: Sun, 03 Apr 2016 14:15:12 GMT
Set-Cookie: PHPSESSID=vfuo4s72b2ffulacumgu9aidf0; path=/
Link: <http://example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://example.com/>; rel=shortlink
Z_LOCATION: PHP MAIN
URI: /index.php
X-Cache: HIT

C:\Users\curl-7.48>curl -I http://example.com
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sun, 03 Apr 2016 14:15:17 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Cached: Sun, 03 Apr 2016 14:15:16 GMT
Set-Cookie: PHPSESSID=fbd37bsvi27cd3hcmrcg4lsio6; path=/
Link: <http://example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://example.com/>; rel=shortlink
Z_LOCATION: PHP MAIN
URI: /index.php
X-Cache: EXPIRED     

C:\Users\curl-7.48>curl -I http://example.com
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sun, 03 Apr 2016 14:15:46 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
X-Cached: Sun, 03 Apr 2016 14:15:43 GMT
Set-Cookie: PHPSESSID=dkngc066edc1apnogulga1mtg4; path=/
Link: <http://example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://example.com/>; rel=shortlink
Z_LOCATION: PHP MAIN
URI: /index.php
X-Cache: HIT

我还有一个简单的 time.php 脚本,它总是显示命中:<?php echo time(); ?>

C:\Users\curl-7.48\bin>curl -I http://example.com/time.php
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Sun, 03 Apr 2016 14:26:17 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4.14
Z_LOCATION: PHP MAIN
URI: /time.php
X-Cache: HIT

请问一下:

  1. 我正在缓存到 /dev/shm/nginx。有没有关于发生了什么以及如何查找故障的指示(内存使用正常,没有内存页面交换到磁盘,inode 正常)?

  2. 这是否与我的 PHP 应用程序返回强制 nginx 不缓存的标头有关?有什么方法可以检测到这种情况?我在 CURL 输出中看不到任何标头(除非我遗漏了某些内容)?

  3. 这个顺序是否fastcgi_pass php;应该位于块的开始或结束处location ~ \.php$

  4. 此外,如何找到其中缓存页面的实际文件time.php... /dev/shm/nginx(有很多第一级目录0x0-0xf,然后是第二级目录0x00 to 0xff)?

  5. 运行时,fatrace我看不到 /dev/shm/nginx 正在被读取 - 我是不是漏掉了什么?nginx 是否将最近读取的文件缓存到本地内存存储中?

  6. 我按照设置缓存日志我的页面没有从缓存中获取服务。但 nginx 实际上正在缓存文件并且看到我收到了很多"GET /all/?source=p1& HTTP/1.1" BYPASS条目"GET /video/aab HTTP/1.1" EXPIRED,这有关系吗?

我的 nginx 配置如下:

http
{
    client_max_body_size 100m;
    proxy_connect_timeout 15s;
    proxy_send_timeout  15s;
    proxy_read_timeout  15s;
    fastcgi_send_timeout 15s;
    fastcgi_read_timeout 15s;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 32 32k;

    fastcgi_cache_path /dev/shm/nginx levels=1:2 keys_zone=MYCACHE:512m max_size=4096m inactive=120m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    server
    {
        server_name example.com;
        root /home/example/public_html/;
        index index.php;

        access_log /home/example/logs/access.log;
        error_log  /home/example/logs/error.log;

        # Rules to work out when cache should or should not be used
        set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
            set $skip_cache 1;
        }

        if ($query_string != "") {
            set $skip_cache 1;
        }

        # Don't cache uris containing the following segments
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            try_files $uri =404;

            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include fastcgi_params;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            fastcgi_cache_valid 200 120m;
            fastcgi_cache_use_stale error timeout invalid_header http_500;
            fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

            fastcgi_cache MYCACHE;
            fastcgi_cache_methods GET HEAD;
            fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;

            #more_clear_headers Server; more_clear_headers "Pragma";
            add_header Z_LOCATION "PHP MAIN"; add_header URI $uri; # DEBUG
            add_header X-Cache $upstream_cache_status;

            fastcgi_pass php;
        }

        location ~ /purge(/.*) {
            fastcgi_cache_purge MYCACHE "$scheme$request_method$host$1";
        }
    }
}

相关内容