簡體   English   中英

如何阻止Varnish緩存Sitemap?

[英]How to stop Varnish from caching Sitemap?

我正在Nginx和Varnish上運行Wordpress博客。 我為Varnish使用以下配置:

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 
backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
    .max_connections = 800;
}


acl purge {
        "localhost";
}

sub vcl_recv {
    set req.grace = 2m;

  # Set X-Forwarded-For header for logging in nginx
  remove req.http.X-Forwarded-For;
  set    req.http.X-Forwarded-For = client.ip;


  # Remove has_js and CloudFlare/Google Analytics __* cookies.
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
  # Remove a ";" prefix, if present.
  set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");



# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
        # Don't cache, pass to backend
        return (pass);
}


# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

# Static content unique to the theme can be cached (so no user uploaded images)
# The reason I don't take the wp-content/uploads is because of cache size on bigger blogs
# that would fill up with all those files getting pushed into cache
if (req.url ~ "wp-content/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") {
    unset req.http.cookie;
}

# Even if no cookies are present, I don't want my "uploads" to be cached due to their potential size
if (req.url ~ "/wp-content/uploads/") {
    return (pass);
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
        # A wordpress specific cookie has been set
    return (pass);
}



    # allow PURGE from localhost
    if (req.request == "PURGE") {
        if (!client.ip ~ purge) {
            error 405 "Not allowed.";
        }
        return (lookup);
    }


    # Force lookup if the request is a no-cache request from the client
    if (req.http.Cache-Control ~ "no-cache") {
        return (pass);
    }


# Try a cache-lookup
return (lookup);

}

sub vcl_fetch {
    #set obj.grace = 5m;
    set beresp.grace = 2m;

}

sub vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

sub vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

我已遵循此處提到的教程

一切正常,但是我在每個新帖子發布后都使用Yoast SEO插件動態生成Sitemap。 它會生成一個名為sitemap_index.xml的站點地圖索引,其中包含其他站點地圖(用於帖子,頁面,作者等)。 這也很好。

  1. 問題是如何防止Varnish緩存站點地圖?
  2. 如何防止Varnish與Google Analytics(分析)混淆? 它不應阻止GA向我提供正確的報告。

我是Varnish的新手,有人可以指導我如何修改配置。 :( 請幫忙。

更新:

如果我將以下內容包含在sub vcl_recv它將起作用sub vcl_recv

if (req.url ~ "\.xml(\.gz)?$") {
   return (pass);
}

刪除這些行!

if (req.url ~ "\.xml(\.gz)?$") {
   return (pass);
}

返回(通過)是一種解決方法,但這不是您要使用Varnish的方式。 Varnish在這里用於緩存頁面和內容,例如sitemap_index.xml

您已經在VCL中實現了PURGE機制,因此處理sitemap_index.xml問題的最簡單方法是對其進行PURGE!

基本原則是,只要未發布新帖子, sitemap_index.xml需要緩存sitemap_index.xml 然后,每次創建新帖子時,您都必須通過發送以下HTTP請求(從官方文檔(1)中粘貼)來通知Varnish sitemap_index.xml不再有效:

PURGE /sitemap_index.xml HTTP/1.0
Host: example.com

因此,我想您可以通過手動編輯模塊或使用Varnish HTTP Purge / WordPress模塊​​(也可以手動修改)來選擇(2)

  1. https://www.varnish-cache.org/docs/3.0/tutorial/purging.html#http-purges

  2. http://wordpress.org/plugins/varnish-http-purge/

如果我將以下內容包含在sub vcl_recv中,它將起作用嗎?

if(req.url〜“ .xml(.gz)?$”){return(pass); }

這將起作用。 將其放在函數頂部附近。 但是請記住,這將防止緩存所有 .xml文件和所有 .xml.gz文件。 當然,您可能正在提供的大多數xml文件和xml.gz文件(站點地圖)仍然是一個考慮因素,以防萬一它們不是。

我無法提供確切的語法,但是您應該通過管道發送對站點地圖的請求。

* pipe-匹配vcl中的請求,並始終將其定向以從服務器獲取。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM