简体   繁体   English

在Varnish缓存的页面中包含req.http.referer

[英]Include req.http.referer in Varnish cached pages

I have a site that has traffic spikes from only a few sources. 我有一个网站的流量峰值仅来自少数几个来源。 This site also needs to be able to show content to a user by guessing if they've just arrived on the site using PHPs $_SERVER["HTTP_REFERER"]. 该网站还需要能够通过猜测用户是否刚刚使用PHP $ _SERVER [“ HTTP_REFERER”]到达网站来向其显示内容。

Is it possible to include the value of req.http.referer in the varnish cache so that this behavior is on more of an individual user basis? 是否可以在清漆缓存中包含req.http.referer的值,以便此行为更多地基于单个用户? I believe that some users are being showed the wrong content because of another user caching content with a different referrer value. 我相信某些用户正在显示错误的内容,因为另一个用户正在缓存具有不同引荐来源值的内容。

If you want the cache to be different on all referers possible the http protocols Vary-header is the solution for you. 如果您希望所有引用服务器上的缓存都不同,则http协议Vary-header是您的解决方案。 That will practically kill your cache hitrate if your traffic is not very uniform with referers from very few unique URLs. 如果您的流量与来自唯一URL的引荐来源的流量不是非常均匀,那实际上将杀死您的缓存命中率。

What I think is a better solution is to remove the logic of guessing who is a new user from the php layer and add it to varnish. 我认为更好的解决方案是从php层中删除猜测谁是新用户的逻辑,并将其添加到清漆中。 This can be done by setting a custom header to the request in the vcl_recv and then using that header to hash in vcl_hash by adding something like this to vcl_recv 可以通过在vcl_recv中为请求设置自定义标头,然后通过向vcl_recv中添加类似的内容,使用该标头在vcl_hash中进行哈希操作来完成此操作。

if ((req.http.referer ~ "www.newuserhost1.com" || req.http.referer ~ "www.newuserhost2.com") &&
    req.url == "/") {
       req.http.X-New-User = 1;
}

And adding this to vcl_hash 并将其添加到vcl_hash

 hash_data(req.http.X-New-User);

When the user reaches your backend you can check the value of X-New-User from there instead of the referer. 当用户到达您的后端时,您可以从那里而不是引用者检查X-New-User的值。

The HTTP specification has the solution for this. HTTP规范为此提供了解决方案。 The Vary response header indicates which request headers was taken into account when producing the content. Vary响应标头指示在生成内容时考虑了哪些请求标头。

Add a "Vary: Referer" header to the backend responses, and Varnish will serve the correct variant to each client. 在后端响应中添加“ Vary:Referer”标头,Varnish将为每个客户端提供正确的变体。

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

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