Nginx 地图缓存规则不起作用

Nginx 地图缓存规则不起作用

我已经为 Nginx 创建了映射规则以避免缓存。但是它不适用于 nocache_map.conf(包含在 http 部分中)。

map $http_x_requested_with $nocache {
        default         0;
        XMLHttpRequest  1;
    }

map $http_cookie $nocache {
        default                     0;
        SESS                        1;
        PHPSESSID                   1;
        ~*wordpress_[a-zA-Z0-9-].*  1;
        comment_author              1;
        wp-postpass                 1;
        _mcnc                       1;
    }

map $request_uri $nocache {
        default                                     0;
        ~*\/wp-admin\/.*                            1;
        ~*\/wp-[a-zA-Z0-9-]+\.php                   1;
        ~*\/feed\/.*                                1;
        ~*\/administrator\/.*                       1;
}

map $http_user_agent $nocache {
    default                                      0;
    ~*android|ip(hone|od)|windows\s+(?:ce|phone) 1;
    ~*symbian|sonyericsson|samsung|lg|blackberry 1;
    ~*mobile                                     1;
}

map $request_method $nocache {
    default 0;
    POST    1; #no caching on post
}

这些规则在nocache.conf中运行良好。

#Cache everything by default
    set $nocache 0;

#Don't cache POST requests
    if ($request_method = POST) { set $nocache 1; }

#Don't cache if the URL contains a query string
    if ($query_string != "") { set $nocache 1; }

# Ajax
    if ($http_x_requested_with = XMLHttpRequest) { set $nocache 1; }

# Wordpress section start

# Don't cache uris containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $nocache 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*|_mcnc") {
   set $nocache 1;
    }

# Wordpress section end

    if ($http_user_agent ~* "android|ip(hone|od)|windows\s+(?:ce|phone)|symbian|sonyericsson|samsung|lg|blackberry|mobile") {
    set $nocache 1;    
    }

服务器配置

server {
    listen      80;
    include     nocache.conf
    ...

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

location ~ ^.+\.php(?:/.*)?$
{
    try_files           $uri $uri/ /index.php$is_args$args;


fastcgi_split_path_info     ^(.+.php)(.*)$;
        fastcgi_no_cache                $nocache $cookie_nocache;
        fastcgi_cache_bypass        $nocache $cookie_nocache;
....

缓存工作正常,只是地图规则不起作用。你能帮我找出错误在哪里吗?谢谢。

答案1

您使用该指令多次声明相同的变量map,因此当 nginx 在fastcgi_no_cachefastcgi_cache_bypass指令上下文中评估该变量时,它将仅使用一个map块。

声明不同的变量并在这些指令中使用它们。

相关内容