Skip to content

Commit

Permalink
Merge pull request magento#557 from magento/kh_2.2_varnish1
Browse files Browse the repository at this point in the history
Remove smart quotes
  • Loading branch information
keharper authored Feb 22, 2017
2 parents 30dc90b + 0579f9d commit f755ba5
Show file tree
Hide file tree
Showing 12 changed files with 698 additions and 25 deletions.
Binary file added common/images/config_varnish_admin_22.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 7 additions & 8 deletions guides/v2.0/config-guide/varnish/config-varnish-configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ subgroup: 09_Varnish
title: Configure Varnish and your web server
menu_title: Configure Varnish and your web server
menu_order: 10
menu_node:
menu_node:
version: 2.0
github_link: config-guide/varnish/config-varnish-configure.md
---

<h2 id="config-varnish-config-web">Configure your web server</h2>
Configure your web server to listen on a port other than the default port 80 because Varnish responds directly to incoming HTTP requests, not the web server.
Configure your web server to listen on a port other than the default port 80 because Varnish responds directly to incoming HTTP requests, not the web server.

In the sections that follow, we use port 8080 as an example.

Expand Down Expand Up @@ -66,24 +65,24 @@ To minimally configure Varnish:
.port = "80";
}

4. Replace the value of `.host` with the fully qualified host name or IP address and listen port of the Varnish *backend* or *origin server*; that is, the server providing the content Varnish will accelerate.
4. Replace the value of `.host` with the fully qualified host name or IP address and listen port of the Varnish *backend* or *origin server*; that is, the server providing the content Varnish will accelerate.

Typically, this is your web server.
Typically, this is your web server.

<a href="https://www.varnish-cache.org/docs/trunk/users-guide/vcl-backends.html" target="_blank">More information</a>
5. Replace the value of `.port` with the web server's listen port (8080 in this example).

Example: Apache is installed on host 192.0.2.55 and Apache is listening on port 8080:

backend default {
.host = "192.0.2.55";
.host = "192.0.2.55";
.port = "8080";
}
}

<div class="bs-callout bs-callout-info" id="info">
<p>If Varnish and Apache are running on the same host, we recommend you use an IP address or host name and not <code>localhost</code>.</p>
</div>

7. Save your changes to `default.vcl` and exit the text editor.

8. Restart Varnish:
Expand Down
2 changes: 1 addition & 1 deletion guides/v2.0/config-guide/varnish/config-varnish-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ subgroup: 09_Varnish
title: Install Varnish
menu_title: Install Varnish
menu_order: 5
menu_node:
menu_node:
version: 2.0
github_link: config-guide/varnish/config-varnish-install.md
---
Expand Down
8 changes: 4 additions & 4 deletions guides/v2.0/config-guide/varnish/config-varnish-magento.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ subgroup: 09_Varnish
title: Configure Magento to use Varnish
menu_title: Configure Magento to use Varnish
menu_order: 15
menu_node:
menu_node:
version: 2.0
github_link: config-guide/varnish/config-varnish-magento.md
---
Expand All @@ -30,7 +30,7 @@ To configure Magento to use Varnish:
<td>Access list</td>
<td><p>Enter the fully qualified host name, IP address, or <a href="https://www.digitalocean.com/community/tutorials/understanding-ip-addresses-subnets-and-cidr-notation-for-networking" target="_blank">Classless Inter-Domain Routing (CIDR)</a> notation IP address range for which to invalidate content.</p>
<p><a href="https://www.varnish-cache.org/docs/3.0/tutorial/purging.html" target="_blank">More information</a></p></td>
</tr>
</tr>
<tr>
<td>Backend host</td>
<td><p>Enter the fully qualified host name or IP address and listen port of the Varnish <em>backend</em> or <em>origin server</em>; that is, the server providing the content Varnish will accelerate. Typically, this is your web server. </p>
Expand All @@ -40,7 +40,7 @@ To configure Magento to use Varnish:
<td>Backend port</td>
<td>Origin server's listen port.</td>
</tr>

</tbody>
</table>

Expand All @@ -60,7 +60,7 @@ To configure Magento to use Varnish:
acl purge {
"localhost";
}

9. Restart Varnish and your web server:

service varnish restart
Expand Down
10 changes: 5 additions & 5 deletions guides/v2.0/config-guide/varnish/config-varnish.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ github_link: config-guide/varnish/config-varnish.md
<h2 id="config-varnish-over">Overview of the Varnish solution</h2>
<a href="https://www.varnish-cache.org/" target="_blank">Varnish Cache</a> is an open source web application accelerator (also referred to as an *HTTP accelerator* or *caching HTTP reverse proxy*). Varnish stores (or caches) files or fragments of files in memory; this enables Varnish to reduce the response time and network bandwidth consumption on future, equivalent requests. Unlike web servers like Apache and nginx, Varnish was designed for use exclusively with the HTTP protocol.

Magento 2 supports Varnish versions 3.0.5 or later or any Varnish 4.x version.
Magento 2 supports versions 3.0.5 or later or any Varnish 4.x version.

<div class="bs-callout bs-callout-warning">
<p>We <em>strongly recommend</em> you use Varnish in production. The built-in full-page caching (to either the file system or <a href="{{page.baseurl}}config-guide/cache/caching-database.html">database</a>) is much slower than Varnish, and Varnish is designed to accelerate HTTP traffic.</p>
Expand All @@ -31,19 +31,19 @@ The following figure shows a basic view of Varnish in your Magento topology.

<img src="{{ site.baseurl }}common/images/varnish_basic.png" width="400px" alt="Basic Varnish diagram">

In the preceding figure, users' HTTP requests over the internet result in numerous requests for CSS, HTML, JavaScript, and images (referred to collectively as *assets*). Varnish sits in front of the web server and proxies these requests to the web server.
In the preceding figure, users' HTTP requests over the internet result in numerous requests for CSS, HTML, JavaScript, and images (referred to collectively as *assets*). Varnish sits in front of the web server and proxies these requests to the web server.

As the web server returns assets, cacheable assets are stored in Varnish. Any subsequent requests for those assets are fulfilled by Varnish (meaning, the requests don't reach the web server). Varnish returns cached content extremely quickly. The results are faster response times to return the content to users and a reduced number of requests that must be fulfilled by Magento.
As the web server returns assets, cacheable assets are stored in Varnish. Any subsequent requests for those assets are fulfilled by Varnish (meaning, the requests don't reach the web server). Varnish returns cached content extremely quickly. The results are faster response times to return the content to users and a reduced number of requests that must be fulfilled by Magento.

Assets cached by Varnish expire at a configurable interval or are replaced by newer versions of the same assets. You can also clear the cache manually either using the Magento Admin or the <a href="{{page.baseurl}}/config-guide/cli/config-cli-subcommands-cache.html">`magento cache:clean`</a> command.
Assets cached by Varnish expire at a configurable interval or are replaced by newer versions of the same assets. You can also clear the cache manually either using the Magento Admin or the <a href="{{page.baseurl}}/config-guide/cli/config-cli-subcommands-cache.html">`magento cache:clean`</a> command.

<h2 id="config-varnish-process">Process overview</h2>
This topic discusses how to initially install Varnish with a minimal set of parameters and test that it works. Then you'll export a Varnish configuration from the Magento Admin and test it again.

The process can be summarized as follows:

1. Install Varnish and test it by accessing any Magento page to see if you're getting HTTP response headers that indicate Varnish is working.
2. Install the Magento software and use the Magento Admin to create a Varnish configuration file.
2. Install the Magento software and use the Magento Admin to create a Varnish configuration file.
3. Replace your existing Varnish configuration file with the one generated by the Admin.
3. Test everything again.

Expand Down
4 changes: 2 additions & 2 deletions guides/v2.0/config-guide/varnish/tshoot-varnish-503.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ subgroup: 09_Varnish
title: Troubleshooting 503 (Backend Fetch Failed) errors
menu_title: Troubleshooting 503 (Backend Fetch Failed) errors
menu_order: 500
menu_node:
menu_node:
version: 2.0
github_link: config-guide/varnish/tshoot-varnish-503.md
---
Expand All @@ -32,7 +32,7 @@ To resolve this issue, increase the default value of `http_resp_hdr_len` in your
For example:

-p http_resp_hdr_len=64000 \

A snippet follows:

# DAEMON_OPTS is used by the init script.
Expand Down
176 changes: 176 additions & 0 deletions guides/v2.2/config-guide/varnish/config-varnish-advanced.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
---
layout: default
group: config-guide
subgroup: 09_Varnish
title: Advanced Varnish configuration
menu_title: Advanced Varnish configuration
menu_order: 16
menu_node:
version: 2.2
github_link: config-guide/varnish/config-varnish-advanced.md
---

Varnish provides several features that prevent customers from experiencing long delays and timeouts when the Magento server is not functioning properly. These features can be configured in the `default.vcl` file. This topic describes the additions that Magento provides in the VCL (Varnish Configuration Language) file you download from Magento Admin.

See the [Varnish Reference Manual](https://www.varnish-cache.org/docs/4.1/reference/index.html) for details about using the Varnish Configuration Language.

## Health check {#health}
Varnish's health check feature polls the Magento server to determine whether it is responding in a timely manner. If it is responding normally, fresh content will be regenerated after the Time to Live (TTL) period has expired. If not, Varnish always serves stale content.

Magento defines the following default health check:

{% highlight json %}
.probe = {
.url = "/pub/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
{% endhighlight %}

Every 5 seconds, this health check calls the `pub/health_check.php` script. This script checks the availability of the server, each database, and Redis (if installed). The script must return a response within 2 seconds. If the script determines that any of these resources are down, it returns a 500 HTTP error code. If this error code is received in 6 out of 10 attempts, the backend is considered unhealthy.

The `health_check.php` script is located in the `pub` directory. If your Magento root directory is `pub`, then be sure to change the path in the `url` parameter from `/pub/health_check.php` to `health_check.php`.

For more information, see the <a href="https://www.varnish-cache.org/docs/4.1/usrs-guide/vcl-backends.html#health-checks" target="_blank">Varnish health checks</a> documentation.

## Grace mode {#grace}

Grace mode enables Varnish to keep an object in cache beyond its TTL value. Varnish can then serve the expired (stale) content while it fetches a new version. This improves the flow of traffic and decreases load times. It's used in the following situations:

* When the Magento backend is healthy, but a request is taking longer than normal
* When the Magento backend is not healthy.

The `vcl_hit` subroutine defines how Varnish responds to a request for objects that have been cached.

### When the Magento backend is healthy {#grace-healthy}


When the health checks determine that the Magento backend is healthy, Varnish checks whether time remains in the grace period. The default grace period is 300 seconds, but a merchant can set the value from Admin as described in [Configure Magento to use Varnish]({{page.baseurl}}config-guide/varnish/config-varnish-magento.html). If the grace period hasn't expired, Varnish delivers the stale content, and asynchronously refreshes the object from the Magento server. If the grace period has expired, Varnish serves the stale content and synchronously refreshes the object from the Magento backend.

The maximum amount of time that Varnish serves a stale object is the sum of the grace period (300 seconds by default) and the TTL value (86400 seconds by default).

To change the default grace period from within the `default.vcl` file, edit the following line in the `vcl_hit` subroutine:

`if (obj.ttl + 300s > 0s) {`

### When the Magento backend is not healthy {#grace-unhealthy}

If the Magento backend is not responsive, Varnish serves stale content from cache for three days (or as defined in `beresp.grace`) plus the remaining TTL (which by default is one day), unless the cached content is manually purged.

## Saint mode {#saint}

Saint mode blacklists unhealthy backends for a configurable amount of time. As a result, unhealthy backends cannot serve traffic when using Varnish as a load balancer. Saint mode can be used in conjunction with grace mode to allow for complex handling of unhealthy backend servers. For example, if one backend server is unhealthy, retries can be routed to another server. If all other servers are down, then serve stale cached objects. The saint mode backend hosts and blackout periods are defined in the `default.vcl` file.

Saint mode can also be used when Magento instances are individually taken offline to perform maintenance and upgrade tasks without affecting the availability of the Magento site.

### Saint mode prerequisites {#saint-prereq}
You should designate one machine as the primary installation. On this machine, install the main instance of Magento, mySQL database, and Varnish.

On all other machines, the Magento instance must have access the primary machine's mySQL database. The secondary machines should also have access to the files of the primary Magento instance.

Alternatively, static files versioning can be turned off on all machines. This can be accessed from the Admin under **Stores > Configuration > Advanced > Developer > Static Files Settings > Sign Static Files** = **No**.

Finally, all Magento instances must be in production mode. Before Varnish starts, clear the cache on each instance. In Admin, go to **System > Cache Management** and click **Flush Magento Cache**. You can also run the following command to clear the cache:

`bin/magento cache:flush`

### Installation {#saint-install}

Saint mode is not part of the main Varnish package. It is a separately-versioned vmod that must be downloaded and installed. As a result, you should re-compile Varnish from source, as described in the following articles:

* [Installing Varnish 5.0](https://www.varnish-cache.org/docs/5.0/installation/install.html)
* [Installing Varnish 4.1](https://www.varnish-cache.org/docs/4.1/installation/install.html)
* [Installing Varnish 4.0](https://www.varnish-cache.org/docs/4.0/installation/install.html)

After you've recompiled, you can install the Saint mode module. In general, follow these steps:

<ol>
<li><p>Obtain the source code from <a href="https://github.com/varnish/varnish-modules">Varnish modules</a> . Clone the git version (master version) since the 0.9.x versions contain a source code error.</p></li>

<li><p>Build the source code with autotools:</p>

<p><code>sudo apt-get install libvarnishapi-dev || sudo yum install varnish-libs-devel</code></p>
<p><code>./bootstrap # If running from git.</code></p>
<p><code>./configure</code></p>
<p><code>make</code></p>
<p><code>make check # optional</code></p>
<p><code>sudo make install</code></p>
</li></ol>

See [Varnish module collection](https://github.com/varnish/varnish-modules) for information about installing the Saint mode module.

### Sample `vcl` file {#saint-sample}

The following code example shows the code that must be added to your `.vcl` file to enable saint mode. Place the `import` statements and `backend` definitions at the top of the file.

{% collapsible Click to show/hide %}
{% highlight cpp %}

import saintmode;
import directors;

backend default1 {
.host = "192.168.0.1";
.port = "8080";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
}

backend default2 {
.host = "192.168.0.2";
.port = "8080";
.first_byte_timeout = 600s;
.probe = {
.url = "/pub/health_check.php";
.timeout = 2s;
.interval = 5s;
.window = 10;
.threshold = 5;
}
}

sub vcl_init {
# Instantiate sm1, sm2 for backends tile1, tile2
# with 10 blacklisted objects as the threshold for marking the
# whole backend sick.
new sm1 = saintmode.saintmode(default1, 10);
new sm2 = saintmode.saintmode(default2, 10);

# Add both to a director. Use sm0, sm1 in place of tile1, tile2.
# Other director types can be used in place of random.
new magedirector = directors.random();
magedirector.add_backend(sm1.backend(), 1);
magedirector.add_backend(sm2.backend(), 1);
}

sub vcl_backend_fetch {
# Get a backend from the director.
# When returning a backend, the director will only return backends
# saintmode says are healthy.
set bereq.backend = magedirector.backend();
}

sub vcl_backend_response {
if (beresp.status >= 500) {
# This marks the backend as sick for this specific
# object for the next 20s.
saintmode.blacklist(20s);
# Retry the request. This will result in a different backend
# being used.
return (retry);
}
}

{% endhighlight %}
{% endcollapsible %}

#### Final step
<a href="{{page.baseurl}}config-guide/varnish/config-varnish-final.html">Final verification</a>

This file was deleted.

Loading

0 comments on commit f755ba5

Please sign in to comment.