Manual:Varnish caching/zh

Varnish 是一个轻量级、高效的反向代理服务器 ，它减少了为经常请求的页面提供服务所需的时间.

Varnish 是一种 HTTP 加速器，它存储由 Web 服务器提供的页面副本. 下次请求同一页面时，Varnish 将提供副本而不是从 Apache 服务器请求页面. 此缓存过程消除了 MediaWiki 再次重新生成同一页面的需要，从而极大地提高了性能.

Varnish 具有专为用作 HTTP 加速器（反向代理）而设计的优势. 它将大部分缓存数据存储在内存中，与更大、更多用途的 包相比，它创建的磁盘文件和对文件系统的访问更少. 与 Squid 一样，它从缓存中向匿名 IP 用户提供经常请求的页面，而不是从原始 Web 服务器请求它们. 这减少了基本 MediaWiki 服务器的 CPU 使用率和数据库访问.

由于这种性能提升，MediaWiki 被设计为与网络缓存紧密集成，并在页面应从缓存中清除以便重新生成时通知 Squid 或 Varnish.

从 MediaWiki 的角度来看，正确配置的 Varnish 安装可以与其对应的 Squid 互换.



架构
下面概述了在单个服务器上设置 Varnish、Apache 和 MediaWiki 的示例. 一个更复杂的缓存策略可以在相同的 Varnish 缓存后面使用多个 Web 服务器（所有这些都可以看起来像一个主机）或者使用独立的服务器来传递 wiki 或图像内容.

对于外界，Varnish 似乎充当 Web 服务器. 实际上，它负责将请求传递给 Apache Web 服务器，但仅在必要时才传递. 在同一台服务器上运行的 Apache 只监听来自本地主机（127.0.0.1）的请求，而 Varnish 只监听服务器外部 IP 地址的请求. 这两个服务都在端口 80 上运行而没有冲突，因为每个服务都绑定到不同的 IP 地址.



配置 Varnish
以下配置适用于 Varnish 版本 4 及更高版本.



配置 MediaWiki
由于 Varnish 正在执行来自本地主机的请求，Apache 将收到“127.0.0.1”作为直接远程地址. 但是，当 Varnish 将请求转发给 Apache 时，它被配置为添加“X-Forwarded-For”标头，以便保留来自外界的远程地址. MediaWiki 必须配置为使用“X-Forwarded-For”标头才能在 special:recentchanges 中正确显示用户地址.

Squid 所需的配置与 Varnish 相同. 确保 LocalSettings.php 文件包含以下行：

请务必将“192.168.0.1”替换为您的 Varnish 缓存正在侦听的 IP 地址. 这些设置有两个目的：
 * 如果从 Varnish 缓存服务器收到请求，MediaWiki 日志需要显示用户的 IP 地址，而不是 Varnish 的 IP 地址. 特殊:最近更改中的每个编辑都被报告为“127.0.0.1”，这几乎是无用的；将该地址列为 Squid/Varnish 服务器会使 MediaWiki 忽略该 IP 地址，而是查看用户 IP 的“x-forwarded-for”标头.
 * 如果 wiki 上的页面或图像发生更改，MediaWiki 将向 中列出的每个服务器发送通知，告诉它丢弃（清除）过时的存储页面.

将 用于需要保持在最近更改之外但不接收 HTTP PURGE 消息的地址. 例如，如果 Apache 和 Squid 分别位于 127.0.0.1 和同一台机器上的外部地址，则无需向 Apache 发送用于 Squid 的“清除”消息. 同样，如果 Squid 正在侦听多个地址，则只向其中一个发送“purge”.

另请参阅 Squid 配置设置了解与 Squid/Varnish 缓存相关的所有设置.

如果您使用 HTTPS，请务必将 设置为与  相同的值，但使用 http:// 协议，以防止清除请求作为 HTTPS 发送，因为 Varnish 不支持 HTTPS.

Some notes
Note that Varnish is an alternative to Squid, but does not replace other portions of a complete MediaWiki caching strategy such as:


 * Pre-compiled PHP code : The default behaviour of PHP under Apache is to load and interpret PHP web scripts each time they are accessed. Installation of a cache such as APC (, then allocate memory by setting  or better in  ) can greatly reduce the amount of CPU time required by Apache to serve PHP content.
 * Localisation/Internationalisation : By default, MediaWiki will create a huge  database table and access it constantly - possibly more than doubling the load on the database server after an "upgrade" to the latest MediaWiki version. Set  to force the localisation information to be stored to the file system to remedy this.
 * Variables and session data : Storing variable data such as the MediaWiki sidebar, the list of namespaces or the spam blacklist to a memory cache will substantially increase the speed of a MediaWiki installation. Forcing user login data to be stored in a common location is also essential to any installation in which multiple, interchangeable Apache servers are hidden behind the same Varnish caches to serve pages for the same wikis. Install the memcached package and set the following options in to force both user login information and cached variables to use memcache:
 * Note that, if you have multiple servers, the localhost address needs to be replaced with that of the shared memcached server(s), which must be the same for all of the matching web servers at your site. This ensures that logging a user into one server in the cluster logs them into the wiki on all the interchangeable web servers.
 * Note that, if you have multiple servers, the localhost address needs to be replaced with that of the shared memcached server(s), which must be the same for all of the matching web servers at your site. This ensures that logging a user into one server in the cluster logs them into the wiki on all the interchangeable web servers.
 * Note that, if you have multiple servers, the localhost address needs to be replaced with that of the shared memcached server(s), which must be the same for all of the matching web servers at your site. This ensures that logging a user into one server in the cluster logs them into the wiki on all the interchangeable web servers.
 * Note that, if you have multiple servers, the localhost address needs to be replaced with that of the shared memcached server(s), which must be the same for all of the matching web servers at your site. This ensures that logging a user into one server in the cluster logs them into the wiki on all the interchangeable web servers.

In many cases, there are multiple alternative caching approaches which will produce the same result. See.

Log file
The Apache web server log, by default, shows only the address of the Varnish cache server, in this example "127.0.0.1:80"

Apache may be configured to log the original user's address by capturing "x-forwarded-for" information under a custom log file format.

An example for Apache's httpd.conf to configure logging of x-forwarded-for is:

Image hotlinking
If a site uses Apache's  to block attempts by other websites to hotlink images, this configuration will need to be removed and equivalent configuration added to Varnish's configuration files. Where an image server is located behind Varnish, typically 90% or more of common image requests never reach Apache and therefore will not be blocked by a "http_referer" check in Apache's configurations.



参见

 * Varnish in Layman's Terms
 * Varnish in Layman's Terms
 * Varnish in Layman's Terms