# Discourse behind Varnish?

**URL:** <https://meta.discourse.org/t/discourse-behind-varnish/4864>\
**Category:** Self-hosting\
**Tags:** hosting\
**Created:** [March 18, 2013, 11:48am UTC](https://meta.discourse.org/t/discourse-behind-varnish/4864 "2013-03-18T11:48:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![richp10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richp10/32/104381_2.png) [@richp10](https://meta.discourse.org/u/richp10)\
**Post date:** [March 18, 2013, 3:16pm UTC](https://meta.discourse.org/t/discourse-behind-varnish/4864/2 "2013-03-18T15:16:16Z")

</div>

I have it working very well - as far as I can see Discourse behaves well towards Varnish and you can aggressively cache everything under the /assets directory.

```
 backend discourse {
     .host = "x.x.x.x";
     .port = "81";
 }

sub vcl_recv {

        if (req.http.host == "forum.myhost.org.uk") {
                set req.backend = discourse;
        }

        if (req.http.host == "forum.myhost.org.uk") {
                if (req.url ~ "^/assets") {
                        return(lookup);
                } else {
                        return(pipe);
                }
        }

```

Obviously there is more in my varnish config, but that is the basic / cache specific bit. I proxy through Apache, so in the virtual host have something like this:

```
 <LocationMatch "^/assets/.*$">
    Header set Cache-Control "public, max-age = 3600"
 </LocationMatch>

```

In my setup, the max-age is used to tell varnish to cache, though different cache headings can be used.

There might be a few other things that can be safely cached (comments anyone?) though the above seems to work great.

It takes load off Apache, on which I run mod\_security so the stack is both fast and pretty secure 😄 )

Enjoy…

---

_[View the full topic](https://meta.discourse.org/t/discourse-behind-varnish/4864)._
