防止Varnish双缓存与N子域/规范化主机仅用于清漆

防止Varnish双缓存与N子域/规范化主机仅用于清漆

问题描述:

I have a bunch of subdomains that look like:

http://u-c2f86nr3pa.example.org/?s=1
http://u-v1cfu8dsta.example.org/?s=1

Their content is different, but the static files CSS/JS/Favicon are the same. I want to normalize the hostname for them, but I do not want to normalize it for the PHP script as it works based on the subdomain.

Is there a way I can teach varnish that these domains are common, and static should be cached once for them?

我有一堆子域,如下所示: p>

   http://u-c2f86nr3pa.example.org/?s=1
http://u-v1cfu8dsta.example.org/?s=1
 代码>  PRE> 
 
 

它们的内容不同,但静态文件CSS / JS / Favicon是相同的。 我想规范化它们的主机名,但我不想为PHP脚本规范它,因为它基于子域工作。 p>

有没有办法可以教清漆这些 域是常见的,静态应该缓存一次吗? p> div>

You can conditionally rewrite the Host header in the vcl_recv section of your Varnish configuration. You will need a way to identify which URLs are for static content. Here is an example; the specific regular expressions will depend on how your site is structured.

sub vcl_recv {
    # Normalize Host if URL is one of:
    #     /css/*   /js/*   /favicon*
    if (req.http.Host ~ "^u-\w+\.example\.org$" &&
        req.url ~ "^/(css/|js/|favicon)")
    {
        set req.http.Host = "u-xxxxxxxxx.example.org";
    }
}

Your backend web server will see the rewritten Host, so make sure you normalize to a name that it recognizes.