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 及更高版本.

Configuring MediaWiki
Since Varnish is doing the requests from localhost, Apache will receive "127.0.0.1" as the direct remote address. However, as Varnish forwards requests to Apache, it is configured to add the "X-Forwarded-For" header so that the remote address from the outside world is preserved. MediaWiki must be configured to use the "X-Forwarded-For" header in order to correctly display user addresses in special:recentchanges.

The required configuration is the same for Squid as for Varnish. Make sure the LocalSettings.php file contains the following lines:

Be sure to replace '192.168.0.1' with the IP address on which your Varnish cache is listening. These settings serve two purposes:
 * If a request is received from the Varnish cache server, the MediaWiki logs need to display the IP address of the user, not that of Varnish. A special:recentchanges in which every edit is reported as '127.0.0.1' is all but useless; listing that address as a Squid/Varnish server tells MediaWiki to ignore the IP address and instead look at the 'x-forwarded-for' header for the user's IP.
 * If a page or image is changed on the wiki, MediaWiki will send notification to every server listed in telling it to discard (purge) the outdated stored page.

Use for addresses which need to be kept out of recentchanges, but which do not receive HTTP PURGE messages. For instance, if Apache and Squid are respectively on 127.0.0.1 and an external address on the same machine, there's no need to send Apache a "purge" message intended for Squid. Likewise, if Squid is listening to multiple addresses, only send "purge" to one of them.

See also Squid configuration settings for all settings related to Squid/Varnish caching.

If you use HTTPS, be sure to set to the same value as  but with http:// protocol, to prevent purge requests from being sent as HTTPS, since varnish doesn't support 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