<blockquote>
  <p>I've tried doing this with headers and cookies, but haven't had any luck yet. What I think is happening is that these things are set after the request is made.</p>
</blockquote>

<p>You are correct. <code>fastcgi_cache</code> is basically a switch, is it on or off. Your backend has no say in that because nginx doesn't look to the backend at all to figure out whether it should serve the request via cache or fire up FPM. It looks via the things it does have access to at the time... location, query_string, user_agent, etc. </p>

<p>How often does your client need to update?</p>

<p>One of the things that makes nginx fast is config files are only read into memory once. Apache, by contrast, looks for <code>.htaccess</code> files inside every directory in the tree on every request being served. </p>

<p>To make changes to a config requires nginx to be reloaded (or restarted). Complicating things, port 80 is a privileged port which means to open it up/listen on it, you need to have root permissions. That's not something you would want to give to a user via a web interface.</p>

<p>However, there are some workarounds. </p>

<ul>
<li><strong>If you are able to name your slugs differently in advance</strong> (e.g. append or prefix "-form" or "-dyn" or something like that to the URL), you can use that to tell nginx not to load from cache.  You could even have Craft generate part of the URL for you based on the value of a "Does this have dynamic content?" lightswitch. </li>
<li><strong>Generate part of the nginx config dynamically by using a cron job</strong> which runs a shell script every 15 mins or so to check if there are configuration changes. If there are changes, reload nginx. I'd recommend using a <a href="https://www.ignoredbydinosaurs.com/posts/236-cool-nginx-feature-of-the-week" rel="nofollow noreferrer">map</a>, put that in a separate file, and then include it so you're not touching of the main config files.</li>
<li><strong>Use the <a href="https://github.com/openresty/lua-nginx-module#readme" rel="nofollow noreferrer">lua module</a>.</strong> Lua is a scripting language that lets you run dynamic code on every request. I haven't used it so I don't know how performant it would be but it could work to set variables, etc. </li>
<li><strong>Load content in via JavaScript.</strong> Depending on what you're trying to do, ignore the web server altogether and load content dynamically. If it's an interactive thing that's going to be tough for google to cache anyway, that could work well here.</li>
</ul>

<p>Caching is super tricky. <a href="https://varnish-cache.org" rel="nofollow noreferrer">Varnish</a> has a lot more options when it comes to dynamic content but it's potentially another dameon you need to setup and maintain. nginx is great because of its simplicity, however, that simplicity comes at a cost of flexibility.</p>

CC BY-SA 3.0
2017-10-12T18:04:58.573Z
Thanks for the explanation, @ritterknight. I think I'll talk to the client about just letting us know when they have a page or section that should be excluded and we'll just edit the config manually.

Going forward I'll probably do more to rely on Javascript for dynamic sections, but this is a bit of an older site where it's too much work to refactor some of these sections.
Will Browar
CC BY-SA 3.0
2017-10-12T15:12:22.563Z
2017-10-12T15:36:23.467Z
2017-10-12T15:36:23.467Z
RitterKnight
<p>To the best of my knowledge it is not possible to skip/exclude caching based on the response with fastcgi cache.</p>

CC BY-SA 3.0
2017-10-12T12:16:55.820Z
2017-10-12T12:16:55.820Z
Erik Weber
<p>I've got a Craft 2 site on an NGINX server that uses FastCGI Caching (based on <a href="https://nystudio107.com/blog/static-caching-with-craft-cms" rel="nofollow noreferrer">Static Page Caching with Craft CMS</a>), but I've got a few pages that have some dynamic content that I'd like to exclude from the cache. I understand I can do this within the NGINX config, but my client has asked if they can somehow manage this within Craft.</p>

<p>I've tried doing this with headers and cookies, but haven't had any luck yet. What I think is happening is that these things are set after the request is made.</p>

<p>Is there a way to use a plugin that can get information on which path to exclude into my NGINX config on a per-path basis? If so, where would be a good place to start?</p>

<cache><config><nginx>
Exclude path in NGINX config (for FastCGI Caching)
# Exclude path in NGINX config (for FastCGI Caching)

(Asked by: username_0 on 2017-10-11)

I've got a Craft 2 site on an NGINX server that uses FastCGI Caching (based on [Static Page Caching with Craft CMS](https://nystudio107.com/blog/static-caching-with-craft-cms)), but I've got a few pages that have some dynamic content that I'd like to exclude from the cache. I understand I can do this within the NGINX config, but my client has asked if they can somehow manage this within Craft.

I've tried doing this with headers and cookies, but haven't had any luck yet. What I think is happening is that these things are set after the request is made.

Is there a way to use a plugin that can get information on which path to exclude into my NGINX config on a per-path basis? If so, where would be a good place to start?

**Tags:** cache, config, nginx

---

## 2 answers

### Answer by username_1 on 2017-10-12. Score: 2 | Accepted answer

> I've tried doing this with headers and cookies, but haven't had any luck yet. What I think is happening is that these things are set after the request is made.

You are correct. `fastcgi_cache` is basically a switch, is it on or off. Your backend has no say in that because nginx doesn't look to the backend at all to figure out whether it should serve the request via cache or fire up FPM. It looks via the things it does have access to at the time... location, query\_string, user\_agent, etc.

How often does your client need to update?

One of the things that makes nginx fast is config files are only read into memory once. Apache, by contrast, looks for `.htaccess` files inside every directory in the tree on every request being served.

To make changes to a config requires nginx to be reloaded (or restarted). Complicating things, port 80 is a privileged port which means to open it up/listen on it, you need to have root permissions. That's not something you would want to give to a user via a web interface.

However, there are some workarounds.

* **If you are able to name your slugs differently in advance** (e.g. append or prefix "-form" or "-dyn" or something like that to the URL), you can use that to tell nginx not to load from cache. You could even have Craft generate part of the URL for you based on the value of a "Does this have dynamic content?" lightswitch.
* **Generate part of the nginx config dynamically by using a cron job** which runs a shell script every 15 mins or so to check if there are configuration changes. If there are changes, reload nginx. I'd recommend using a [map](https://www.ignoredbydinosaurs.com/posts/236-cool-nginx-feature-of-the-week), put that in a separate file, and then include it so you're not touching of the main config files.
* **Use the [lua module](https://github.com/openresty/lua-nginx-module#readme).** Lua is a scripting language that lets you run dynamic code on every request. I haven't used it so I don't know how performant it would be but it could work to set variables, etc.
* **Load content in via JavaScript.** Depending on what you're trying to do, ignore the web server altogether and load content dynamically. If it's an interactive thing that's going to be tough for google to cache anyway, that could work well here.

Caching is super tricky. [Varnish](https://varnish-cache.org) has a lot more options when it comes to dynamic content but it's potentially another dameon you need to setup and maintain. nginx is great because of its simplicity, however, that simplicity comes at a cost of flexibility.

> **Comments:**
> **username_0**: Thanks for the explanation, @ritterknight. I think I'll talk to the client about just letting us know when they have a page or section that should be excluded and we'll just edit the config manually.  Going forward I'll probably do more to rely on Javascript for dynamic sections, but this is a bit of an older site where it's too much work to refactor some of these sections.  

---

### Answer by username_2 on 2017-10-12. Score: 1

To the best of my knowledge it is not possible to skip/exclude caching based on the response with fastcgi cache.


---

