php 让清漆在 Magento 上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5029593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Varnish To Work on Magento
提问by Josh Pennington
First please forgive me for total lack of understanding of Varnish. This is my first go at doing anything with Varnish.
首先请原谅我完全不了解Varnish。这是我第一次用 Varnish 做任何事情。
I am following the example at: http://www.kalenyuk.com.ua/magento-performance-optimization-with-varnish-cache-47.html
我正在关注以下示例:http: //www.kalenyuk.com.ua/magento-performance-optimization-with-varnish-cache-47.html
However when I install and run this, Varnish does not seem to cache. I do get the X-Varnish header with a single number and a Via header that has a value of 1.1 varnish
但是,当我安装并运行它时,Varnish 似乎没有缓存。我确实得到了带有单个数字的 X-Varnish 标头和一个值为 1.1 清漆的 Via 标头
I have been told (by my ISP) it is because of the following cookie that Magento sets:
我被告知(我的 ISP)这是因为 Magento 设置了以下 cookie:
Set-Cookie: frontend=6t2d2q73rv9s1kddu8ehh8hvl6; expires=Thu, 17-Feb-2011 14:29:19 GMT; path=/; domain=XX.X.XX.XX; httponly
Set-Cookie: frontend=6t2d2q73rv9s1kddu8ehh8hvl6; expires=Thu, 17-Feb-2011 14:29:19 GMT; path=/; domain=XX.X.XX.XX; httponly
They said that I either have to change Magento to handle this or configure Varnish to handle this. Since changing Magento is out of the question, I was wondering if someone can give me a clue as to how I would configure Varnish to handle this cookie?
他们说我要么必须改变 Magento 来处理这个问题,要么配置 Varnish 来处理这个问题。由于更改 Magento 是不可能的,我想知道是否有人可以给我一个关于如何配置 Varnish 来处理这个 cookie 的线索?
采纳答案by Mario
http://moprea.ro/2011/may/6/magento-performance-optimization-varnish-cache-3/describes the Magento extension that enables full page cache with varnish. This extension relies on Varnish config published on github.
http://moprea.ro/2011/may/6/magento-performance-optimization-varnish-cache-3/描述了使用 varnish 启用整页缓存的 Magento 扩展。此扩展依赖于 github 上发布的 Varnish 配置。
These are the features already implemented:
这些是已经实现的功能:
- Workable varnish config
- Enable full page caching using Varnish, a super fast caching HTTP reverse proxy.
- Varnish servers are configurable in Admin, under System / Configuration / General - Varnish Options
- Automatically clears (only) cached pages when products, categories and CMS pages are saved.
- Adds a new cache type in Magento Admin, under System / Cache Management and offers the possibility to deactivate the cache and refresh the cache.
- Notifies Admin users when a category navigation is saved and Varnish cache needs to be refreshed so that the menu will be updated for all pages.
- Turns off varnish cache automatically for users that have products in the cart or are logged in, etc.)
- Default varnish configuration offered so that the module is workable. Screen shots: https://github.com/madalinoprea/magneto-varnish/wiki
- 可行的清漆配置
- 使用 Varnish 启用整页缓存,这是一种超快速缓存 HTTP 反向代理。
- Varnish 服务器可在 Admin 中配置,在 System / Configuration / General - Varnish Options 下
- 保存产品、类别和 CMS 页面时自动清除(仅)缓存页面。
- 在 Magento Admin 中的 System / Cache Management 下添加新的缓存类型,并提供停用缓存和刷新缓存的可能性。
- 在保存类别导航并且需要刷新 Varnish 缓存时通知管理员用户,以便为所有页面更新菜单。
- 为购物车中有产品或已登录等的用户自动关闭清漆缓存。)
- 提供默认清漆配置,以便模块可用。屏幕截图:https: //github.com/madalinoprea/magneto-varnish/wiki
回答by mattnthat
How to cache Magento in Varnish (Theory) - There are 2 components to this
如何在 Varnish 中缓存 Magento(理论) - 这有 2 个组件
1) Static assets (eg. Images, CSS, JS) - This is a simple common pattern that involves detecting requests that belong to this category and setting a cache time (Or relying on the cache time being sent by the origin server) Example of this in gist form
1)静态资产(例如图像、CSS、JS)——这是一个简单的常见模式,涉及检测属于此类的请求并设置缓存时间(或依赖于源服务器发送的缓存时间) 示例这是要点形式
2) HTML documents - This is the much more complex part of a good Magento solution. Its critical to cache HTML documents in Varnish to be able to improve Magento performance. HTML document generation is the most expensive (time consuming) thing that a Magento server will do.
2) HTML 文档——这是一个好的 Magento 解决方案中复杂得多的部分。在 Varnish 中缓存 HTML 文档以提高 Magento 性能至关重要。HTML 文档生成是 Magento 服务器所做的最昂贵(耗时)的事情。
The challenge with caching HTML documents comes from personalised content.
缓存 HTML 文档的挑战来自个性化内容。
Personalised content and HTML documents
个性化内容和 HTML 文档
Magento, and all other ecommerce sites, manage the state of a particular user though a session. A session is a record of that particular user's status on your site. This covers things such as: “Hello Bob” - at the top of the page “4 Things in Your Cart” - the status of your shopping cart on each page
Magento 和所有其他电子商务网站通过会话管理特定用户的状态。会话是该特定用户在您网站上的状态记录。这包括以下内容:“Hello Bob” - 在页面顶部 “您购物车中的 4 件商品” - 每页上您的购物车状态
These are items that cannot be shared amongst users and would cause a major problem should this happen (we call this “session leakage”).
这些是不能在用户之间共享的项目,如果发生这种情况会导致重大问题(我们称之为“会话泄漏”)。
How do we cache HTML pages if the HTML pages contain personalised information about who the person is and what is in their shopping cart?
如果 HTML 页面包含有关此人是谁以及他们的购物车中有什么的个性化信息,我们如何缓存 HTML 页面?
There are 2 main methods of achieving this: Loading personalised elements of the page via additional requests after the page has loaded A common implementation method here is to use AJAX to request page elements that are personalised Leveraging a technology to mark components of the HTML document as cacheable and other's uncachable (or un shareable amongst users). Varnish supports a technology called ESI (Edge Side Includes) that allows different parts of a HTML document to be cached differently.
实现这一点的主要方法有两种: 在页面加载后通过附加请求加载页面的个性化元素 这里的一个常见实现方法是使用 AJAX 请求个性化页面元素 利用一种技术将 HTML 文档的组件标记为可缓存和其他不可缓存(或无法在用户之间共享)。Varnish 支持一种称为 ESI(Edge Side Includes)的技术,该技术允许对 HTML 文档的不同部分进行不同的缓存。
Your Varnish implementation strategy must factor in user personalisation.
您的 Varnish 实施策略必须考虑用户个性化。
Implementation options for Varnish
清漆的实现选项
Magento 1.X - The most widely used method for caching HTML documents in Magento version 1 is the open source product called Magento Turpentine (by Nexus). This is a plugin that is installed (via Magento Connect) and will automatically add ESI tags to your HTML documents so that Varnish can cache these resources. Magento Turpentine install / guide
Magento 1.X - Magento 版本 1 中最广泛使用的缓存 HTML 文档的方法是名为 Magento Turpentine(由 Nexus)的开源产品。这是一个已安装的插件(通过 Magento Connect),它会自动将 ESI 标签添加到您的 HTML 文档中,以便 Varnish 可以缓存这些资源。Magento 松节油安装/指南
Magento 2.X - The latest version of Magento (currently in beta) supports Varnish as its recommended solution for HTML caching in production. This is great news, Varnish is the recommended option from Magento and will work out of the box to improve your site speed.
Magento 2.X - Magento 的最新版本(目前处于测试阶段)支持 Varnish 作为其在生产中的 HTML 缓存的推荐解决方案。这是个好消息,Varnish 是 Magento 的推荐选项,可以开箱即用,以提高您的网站速度。
How to make Varnish and Magento work well
如何使 Varnish 和 Magento 运行良好
Deployment is one thing, The next steps once you have a Varnish Magento solution implemented and working is to understand how well its performing. Getting metrics on cache hit rates and detailed logs on a request by request basis can be a challenge as it involves deploying a range of additional infrastructure (or being stuck doing manual log collection on a one off basis).
部署是一回事,一旦您实施并运行了 Varnish Magento 解决方案,接下来的步骤就是了解其性能如何。获取有关缓存命中率的指标和逐个请求的详细日志可能是一项挑战,因为它涉及部署一系列额外的基础设施(或在一次关闭的基础上进行手动日志收集)。
We have recently built this infrastructure to run Varnish as a service in the cloud (with full logs/metrics) - www.section.io- Plug aside this can be the most important element to actual success with you Varnish and Magento project as you need to be constantly tuning your implementation to manage things like varying query strings in urls (Hello google analytics "gclid"!) which can reduce your cache hit rates dramatically
我们最近建立了这个基础设施来在云中将 Varnish 作为服务运行(带有完整的日志/指标) - www.section.io- 根据需要,这可能是您在 Varnish 和 Magento 项目中取得实际成功的最重要因素不断调整您的实现以管理诸如 url 中的不同查询字符串之类的事情(您好谷歌分析“gclid”!),这可以显着降低您的缓存命中率
回答by Connor
If you are using Varnish 3.0, you may have to change your .vcl config. Here's what I am using with magento and varnish 3:
如果您使用的是 Varnish 3.0,您可能需要更改您的 .vcl 配置。这是我在 magento 和清漆 3 中使用的内容:
# 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";
}
acl trusted {
"127.0.0.1";
"127.0.1.1";
# Add other ips that are allowed to purge cache
}
#
# http://www.varnish-cache.org/docs/2.1/tutorial/vcl.html#vcl-recv
# @param req Request object
sub vcl_recv {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
}
else {
set req.http.X-Forwarded-For = client.ip;
}
if (req.request == "PURGE") {
# Allow requests from trusted IPs to purge the cache
if (!client.ip ~ trusted) {
error 405 "Not allowed.";
}
ban("req.url ~ " + req.url);
error 200 "Ok"; #We don't go to backend
#return(lookup); # @see vcl_hit
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
# Cache only GET or HEAD requests
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
# parse accept encoding rulesets to normalize
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unkown algorithm
remove req.http.Accept-Encoding;
}
}
# Rules for static files
if (req.url ~ "\.(jpeg|jpg|png|gif|ico|swf|js|css|gz|rar|txt|bzip|pdf)(\?.*|)$") {
set req.http.staticmarker = "1";
unset req.http.Cookie;
return (lookup);
}
# Don't cache pages for Magento Admin
# FIXME: change this rule if you use custom url in admin
if (req.url ~ "^/(index.php/)?admin") {
return(pass);
}
# Don't cache checkout/customer pages, product compare
# if (req.url ~ "^/(index.php/)?(checkout|customer|catalog/product_compare|wishlist)") {
# return(pass);
# }
# Don't cache checkout/customer pages, product compare
if (req.url ~ "/(checkout|customer|catalog/product_compare|wishlist)/") {
return(pass);
}
# Don't cache till session end
if (req.http.cookie ~ "nocache_stable") {
return(pass);
}
# Unique identifier witch tell Varnish use cache or not
if (req.http.cookie ~ "nocache") {
return(pass);
}
# Remove cookie
unset req.http.Cookie;
set req.http.magicmarker = "1"; #Instruct varnish to remove cache headers received from backend
return(lookup);
}
sub vcl_pipe {
# # Note that only the first request to the backend will have
# # X-Forwarded-For set. If you use X-Forwarded-For and want to
# # have it set for all requests, make sure to have:
# # set req.http.connection = "close";
# # here. It is not set by default as it might break some broken web
# # applications, like IIS with NTLM authentication.
return (pipe);
}
#sub vcl_pass {
# return (pass);
#}
#sub vcl_hash {
# set req.hash += req.url;
# if (req.http.host) {
# set req.hash += req.http.host;
# } else {
# set req.hash += server.ip;
# }
# return (hash);
# }
# Called after a cache lookup if the req. document was found in the cache.
sub vcl_hit {
if (req.request == "PURGE") {
ban_url(req.url);
error 200 "Purged";
}
#
# ATTENTION!! I had to comment this to make it work on vernish 3.0!!!!
# error message:
# Symbol not found: 'obj.cacheable' (expected type BOOL):
#
# I'm not sure about it, please check!!!
#
#if (!obj.cacheable) {
# return (pass);
#}
return (deliver);
}
# Called after a cache lookup and odc was not found in cache.
sub vcl_miss {
if (req.request == "PURGE"){
error 200 "Not in cache";
}
return (fetch);
}
# Called after document was retreived from backend
# @var req Request object.
# @var beresp Backend response (contains HTTP headers from backend)
sub vcl_fetch {
set req.grace = 30s;
# Current response should not be cached
if(beresp.http.Set-Cookie ~ "nocache=1") {
return (deliver);
}
# Flag set when we want to delete cache headers received from backend
if (req.http.magicmarker){
unset beresp.http.magicmarker;
unset beresp.http.Cache-Control;
unset beresp.http.Expires;
unset beresp.http.Pragma;
unset beresp.http.Cache;
unset beresp.http.Server;
unset beresp.http.Set-Cookie;
unset beresp.http.Age;
# default ttl for pages
set beresp.ttl = 1d;
}
if (req.http.staticmarker) {
set beresp.ttl = 30d; # static file cache expires in 30 days
unset beresp.http.staticmarker;
unset beresp.http.ETag; # Removes Etag in case we have multiple frontends
}
return (deliver);
}
# Called after a cached document is delivered to the client.
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT ("+obj.hits+")";
} else {
set resp.http.X-Cache = "MISS";
# set resp.http.X-Cache-Hash = obj.http.hash;
}
return (deliver);
}
#
# sub vcl_error {
# set obj.http.Content-Type = "text/html; charset=utf-8";
# synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
# "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
# <head>
# <title>"} obj.status " " obj.response {"</title>
# </head>
# <body>
# <h1>Error "} obj.status " " obj.response {"</h1>
# <p>"} obj.response {"</p>
# <h3>Guru Meditation:</h3>
# <p>XID: "} req.xid {"</p>
# <hr>
# <address>
# <a href="http://www.varnish-cache.org/">Varnish cache server</a>
# </address>
# </body>
# </html>
# "};
# return (deliver);
# }
回答by ZoFreX
I am assuming that's a session cookie that Magento sends to all users - I had a similar problem with Varnish + Redmine.
我假设这是 Magento 发送给所有用户的会话 cookie - 我在使用 Varnish + Redmine 时遇到了类似的问题。
The reason Varnish is not caching your pages is because by default it only caches what it is sure is safe - and users with cookies typically see different things for a given page load, for example if they're logged in then maybe their username is at the top of each page, so the page can't be cached?.
Varnish 不缓存您的页面的原因是因为默认情况下它只缓存它确定安全的内容 - 使用 cookie 的用户通常会看到给定页面加载的不同内容,例如,如果他们已登录,那么他们的用户名可能是每个页面的顶部,所以页面不能被缓存?。
Many frameworks however give session cookies to users that aren't logged in as well. I'm afraid I don't know Magento at all so I cannot predict the consequences of ignoring this cookie - on Redmine, ignoring the cookie meant that users could not log in, and all forms stopped working (because they no longer had the CSRF token).
然而,许多框架也向未登录的用户提供会话 cookie。恐怕我根本不了解 Magento,所以我无法预测忽略此 cookie 的后果 - 在 Redmine 上,忽略 cookie 意味着用户无法登录,并且所有表单都停止工作(因为他们不再拥有 CSRF令牌)。
It would probably be better to tackle this from the Magento side if you can - Varnish will listen to the upstream's headers to determine what can be cached etc.
如果可以的话,最好从 Magento 方面解决这个问题 - Varnish 将侦听上游的标头以确定可以缓存的内容等。
If you can't, then you might be able to mitigate it from Varnish's configuration. You will want to ensure that the Set-Cookie header is not sent from any cache hit, and you will also want to drop the client's cookie on requests for pages where that cookie has no effect. This means you will need exceptions for things such as the login screen, or any page that requires you to be logged in (unless Magento sets a separate cookie once you are logged in, which would make things a lot easier).
如果你不能,那么你可以从 Varnish 的配置中减轻它。您将希望确保 Set-Cookie 标头不会从任何缓存命中发送,并且您还希望在请求该 cookie 无效的页面时删除客户端的 cookie 。这意味着您将需要对登录屏幕或任何需要您登录的页面等例外情况(除非 Magento 在您登录后设置一个单独的 cookie,这会使事情变得容易得多)。
The Varnish documentation (which I can highly recommend as a resource) has several pages on increasing hit rate, including one specifically for dropping cookies on some pages and not others.
Varnish 文档(我强烈推荐作为资源)有几个关于提高命中率的页面,其中一个专门用于在某些页面而不是其他页面上放置cookie。
? There is an exception, which is if you are using edge side includes.
? 有一个例外,那就是如果您使用的是edge side includes。
回答by Phoenix Medien
We have developed a module called PageCache powered by Varnish that enables Magento and Varnish to play smoothly together by providing a well tested Varnish configuration file (VCL) and a tightly integrated Magento module with plenty of options to control Varnish rigth from the Magento backend. Check it out on Magento Connect:
我们开发了一个名为 PageCache 的模块,由 Varnish 提供支持,通过提供经过良好测试的 Varnish 配置文件 (VCL) 和紧密集成的 Magento 模块,该模块能够让 Magento 和 Varnish 顺利地一起运行,该模块具有大量选项来控制来自 Magento 后端的 Varnish rigth。在 Magento Connect 上查看:
http://www.magentocommerce.com/magento-connect/Phoenix/extension/6322/varnish_cache
http://www.magentocommerce.com/magento-connect/Phoenix/extension/6322/varnish_cache
回答by user1743741
This i think explains how we could away with using varnish with magento
我认为这解释了我们如何在 magento 中使用清漆
If you use aoe_static module and my custom vcl for varnish 3 it clears the cookies on the cached page response. It should do this in vcl fetch. The cookies can then be set from a smaller ajax response which loads the dynamic content. This maintains your sessions, cart etc. This ajax response can be "pipe"ed in vcl recovery.
如果您使用 aoe_static 模块和我的自定义 vcl for varnish 3,它会清除缓存页面响应上的 cookie。它应该在 vcl fetch 中执行此操作。然后可以从加载动态内容的较小 ajax 响应中设置 cookie。这会维护您的会话、购物车等。此 ajax 响应可以在 vcl 恢复中“管道化”。
I haven't experienced any problems doing this but haven't tried it on a production site.
我没有遇到任何问题,但没有在生产站点上尝试过。
Dynamic blocks must be replaced with placeholders via layout xml. If you liked these replacements could be varnish edge side includes or custom ajax implementation.
动态块必须通过布局 xml 替换为占位符。如果您喜欢这些替换,可以是清漆边缘包含或自定义 ajax 实现。
When loading the dynamic content from aoe_static (or whatever type ajax methods you prefer) it's good to remember that you can still use magentos layout system, e.g. create a handle for your ajax call with nested blocks to be rendered.
当从 aoe_static(或您喜欢的任何类型的 ajax 方法)加载动态内容时,最好记住您仍然可以使用 magentos 布局系统,例如为您的 ajax 调用创建一个句柄,并带有要呈现的嵌套块。
If you use the aoe_static module you'll notice that the loadLayout is called but remember what handles are passed to that loadLayout. it's not the same as a layout request for the page your on but it does get a default handle for you.
如果您使用 aoe_static 模块,您会注意到 loadLayout 被调用,但请记住传递给该 loadLayout 的句柄。它与您所在页面的布局请求不同,但它确实为您提供了默认句柄。
Another problem is stock levels. When a product no longer has a enough stock to be added to cart it will still appear on product lists and as options for configurable and grouped products.
另一个问题是库存水平。当产品不再有足够的库存可以添加到购物车时,它仍将显示在产品列表中,并作为可配置和分组产品的选项。
Possibly you could use the observer - cataloginventory_stock_item_save_after - to check stock levels (i haven't checked this). Then cache could be purged based on the url of the products. It's quite easy to get the category urls that the product would appear in and purge these at the same time.
可能你可以使用观察者 - cataloginventory_stock_item_save_after - 检查库存水平(我没有检查过这个)。然后可以根据产品的 url 清除缓存。获取产品将出现的类别网址并同时清除这些网址非常容易。
The phoenix module has methods do these sorts of purges if you want to see an easy implementation just follow through from observers.
如果您想从观察者那里看到一个简单的实现,那么 phoenix 模块具有执行此类清除的方法。
But how to deal with the layered nav urls is more tricky. you would need to store the query string parameters that the app had served using the base category list url as a key in advance then read and purge these urls in the observer. This storing of query string parameters would be easy enough using the response before send,checking the request object with a regex and logging the query strings used comma separated.
但是如何处理分层的导航网址就比较棘手了。您需要预先存储应用程序使用基本类别列表 url 作为键提供的查询字符串参数,然后在观察者中读取和清除这些 url。在发送之前使用响应来存储查询字符串参数会很容易,使用正则表达式检查请求对象并记录使用逗号分隔的查询字符串。
I am wrong in thinking that none of the current modules deal with stock levels for layered nav?
我错误地认为当前模块都没有处理分层导航的库存水平?
I think a good finished module for varnish is needed in the open source community since all other are lacking. personally i plan to only use paid full page cache's with load balanced servers and maybe varnish to catch image and css requests. Unless someone wants to join forces to make a proper varnish implementation or I would be happy to assist with issues for your sites if the work could be added to an open source implementation that deals better with all these concerns.
我认为开源社区需要一个好的完成的清漆模块,因为所有其他模块都缺乏。我个人计划只在负载平衡的服务器上使用付费的全页缓存,并且可能使用清漆来捕获图像和 css 请求。除非有人想联合起来进行适当的清漆实施,否则如果可以将工作添加到更好地处理所有这些问题的开源实施中,我将很乐意帮助您解决网站的问题。
checkout this question for more details on problems you will face this question - magento open source full page cache
查看此问题以获取有关您将面临此问题的问题的更多详细信息 - magento 开源全页缓存
回答by NELSON
http://moprea.ro/2011/feb/16/magento-performance-optimization-varnish-cache-2/
http://moprea.ro/2011/feb/16/magento-performance-optimization-varnish-cache-2/
Not sure if it helps but I chanced upon this.
不确定它是否有帮助,但我偶然发现了这一点。
I have actually tried out getting varnish to work before and I have failed. I would suggest you get APC + Memcached + tmpfs sessions/cache before trying out varnish.
实际上,我之前尝试过让清漆起作用,但失败了。我建议你在尝试 varnish 之前获得 APC + Memcached + tmpfs 会话/缓存。