简体   繁体   English

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

[英]prevent Varnish double cache with N subdomains/normalize host only for varnish

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

Is there a way I can teach varnish that these domains are common, and static should be cached once for them? 有没有办法可以教清漆这些域是常见的,静态应该缓存一次?

You can conditionally rewrite the Host header in the vcl_recv section of your Varnish configuration. 您可以有条件地重写Varnish配置的vcl_recv部分中的Host标头。 You will need a way to identify which URLs are for static content. 您需要一种方法来识别哪些URL是静态内容。 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. 您的后端Web服务器将看到重写的主机,因此请确保您标准化为其识别的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM