nginx 不缓存 wordpress 网站

nginx 不缓存 wordpress 网站

我已设置一个使用 nginx 缓存的网站。以下是详细信息:

    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    set $no_cache 0;

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

    if ($query_string != "")
    {
        set $no_cache 1;
    }
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $no_cache 1;
    }

    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $no_cache 1;
    }

    if ($http_cookie = "PHPSESSID")
    {
        set $no_cache 1;
    }


    root /var/www/websites/headstuff.org;
    index index.php;
    server_name _; 


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

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    # Directives to send expires headers and turn off 404 error logging.
    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 365d; #max;
        add_header Cache-Control "public";
    }


    location ~ [^/]\.php(/|$) 
    {
        try_files $uri $uri/ /index.php?q=$uri&$args;

        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
        fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply
        add_header X-Fastcgi-Cache $upstream_cache_status;
        fastcgi_cache_bypass $no_cache;
        fastcgi_no_cache $no_cache;


        # Regular PHP-FPM stuff:
        include fastcgi_params;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_read_timeout 180;

        if (!-f $document_root$fastcgi_script_name) {
           return 404;
        }
   }
}

但当我尝试

curl -X GET -I headstuff.org

我得到以下

HTTP/1.1 301 Moved Permanently
Date: Fri, 05 Feb 2016 10:48:22 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d22f1949b207c61fa8f304a6be8d01e4a1454669298; expires=Sat, 04-Feb-17 10:48:18 GMT; path=/; domain=.headstuff.org; HttpOnly
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Location: http://www.headstuff.org/
Pragma: no-cache
Set-Cookie: PHPSESSID=03eubavpjg3ct5tsiu1g2eqi63; path=/
Vary: Cookie
X-Fastcgi-Cache: MISS
X-Pingback: http://www.headstuff.org/xmlrpc.php
Server: cloudflare-nginx
CF-RAY: 26fe0acb87752999-DUB

总是说 X-Fastcgi-Cache: MISS

知道这里发生什么事了吗?

答案1

首先,您将 nginx 配置为不缓存 301 重定向响应。如果您想缓存这些(您应该这样做!),请进行以下设置:

fastcgi_cache_valid 301 30d;

其次,WordPress 发送了明确指示不缓存重定向,因此无论如何都不会被缓存。

答案2

您 CURL 了错误的 URL,请添加 www。当您这样做时,缓存将起作用,您可以在标头中看到它。

将这些行添加到您的文件中以稍微改变您的缓存。

# Determines in which cases a stale cached response can be used when an error occurs during
# communication with the FastCGI server
fastcgi_cache_use_stale error timeout invalid_header http_500;

# Wordpress themes (especially Photocrati) often send back inappropriate headers, so ignore them
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

我还进行了大量标题重写,因为我的主题设置标题非常糟糕。

相关内容