为啥我 Nginx 下启用 fastcgi_cache 不生成缓存?

先说说我在nginx.confhttp里的配置,如下:

fastcgi_cache_path /var/domain_cache levels=1:2 keys_zone=domain:100m inactive=1d max_size=5G;
fastcgi_temp_path /var/cache_temp;                                                                                                    
fastcgi_cache_key "$scheme$request_method$host$request_uri";                                                                                 #定义哪些情况下用过期缓存
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503 updating;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie Vary;

然后是我的域名myhost.conf文件内的Fastcgi_cache有关配置,如下:

        set $skip_cache 0;

        if ($request_method = POST) {
            set $skip_cache 1;
        }   

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

        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }


        location ~ [^/]\.php(/|$)
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;  
                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;
                add_header X-Cache-Status $upstream_cache_status;
                add_header X-Cache "$upstream_cache_status From $host"; 
                fastcgi_cache domain;
                fastcgi_cache_valid 200 301 302 1d;
        }
        
        location ~ /purge(/.*) {
            allow 127.0.0.1;
            allow "111.112.113.114";
            deny all;
            fastcgi_cache_purge domain "$scheme$request_method$host$1";
        }
    
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off; log_not_found off; expires max;
        }
 
        location = /robots.txt { access_log off; log_not_found off; }
        location ~ /\. { deny  all; access_log off; log_not_found off; }

WordPress 里安装了nginx-helper插件,通过tailf命令查看这个插件生成的.log文件一直是如下显示

图片描述

至于说浏览器里X-Cache查看是否MISS/HIT的根本就不显示,也就是说缓存根本就没有生成。求高手赐教呀!已经折腾我好几天了都!郁闷呀!

阅读 6.3k
3 个回答

发现始终都有一个

pragma:no-cache

的head标记存在,怎么去除这个标记呢?是不是这个造成的fastcgi_cache不能缓存呢?

<meta http-equiv="Pragma" content="no-cache">是用于设定禁止浏览器从本地机的缓存中调阅页面内容,设定后一旦离开网页就无法从Cache中再调出;

时隔N年了,这个问题终于解决了!问题就出在这个Pragma上了,在Nginx里隐藏这个head即可让fastcgi cache生效了!

可参考【开启 Nginx 的 FastCGI Cache 缓存,加速 WordPress 伪静态页面

不要在nginx这里配置缓存策略,如果缓存策略多了,改起来配起来都好麻烦,拿php距离,直接在response的时候返回缓存头信息即可。举例:

    header("Last-Modified:" . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
    header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");
    header("Cache-Control: public, max-age=$offset, pre-check=$offset");    
    header("Content-type: " . $imgInfo['mime']);

核心就是cache-control这里,具体详细的你百度一下了解就好。
推荐缓存策略在服务器端加入header头里面,这样修改和控制起来会方便很多。

  1. nginx不要用if,会造成很多不可预期的结果,除非你能明确知道if的结果。你这用了这么多if,这配置文件基本不可读

  2. 不要轻易使用ignore_header,很容易造成生产事故

  3. 结果是否能缓存,应该由代码来决定,不是由nginx.conf决定

  4. 建议用memcache,WordPress有相关插件

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题