2

I have a simple HTML Single Page Application hosted on cloud foundry using the Staticfile buildpack. Whenever I do a new release using cf push, browsers still show the previous (cached) version when opening the page. I need to manually hit the refresh button on the browser, which i dont want.

How do I configure nginx on CloudFoundry, so a modified html page is always getting reloaded from server instead from cache? I guess the default HTTP caching settings are not properly set for the this szenario in CloudFoundry?

Note: Within the HTML page itself, I already use cache busting with versioned javascript and css file names.

fischermatte
  • 3,093
  • 3
  • 38
  • 50

1 Answers1

3

You'll need to set some the Cache-Control headers in your NGINX config to ask the browser's to not cache your static content. This answer to a more general question gives a pretty good explanation on what exactly you'll need to set:

How to control web page caching, across all browsers?

As for configuring NGINX itself, I've found this gist helpful for explaining the various options available.

Now for the staticfile-buildpack, judging from their some of their issues, it sounds like the easiest way will be to use the "location include" feature which essentially lets you mixin your own custom nginx config (it will end up here in the final generated nginx.conf). I haven't tested this, but I imagine you'll want to include something like:

location <some-location-matcher*> {
  add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
  expires off;
  proxy_no_cache 1;
}

* Not sure if you want to do every file, just html files, or something else, but here's more info on how to use the nginx location matchers.

You should be able to just add a file similar to the snippet above and include it in your Staticfile like this:

location_include: /path-in-app-to-where-you-placed-your-config/filename.conf

It's not directly applicable, but if you want more examples, I wrote this blog post a while back on how I used the Staticfile buildpack's location_include feature to set up custom 404 pages on one of my static sites. They've since apparently added first-class support for that particular feature, but the post does give an example of how I structured my includes and added them to the Staticfile configuration.

Hope that helps!

tcdowney
  • 307
  • 2
  • 5
  • thx, that worked. one thing to mention when doing this is, that one need to set also an alternative root path in the staticfile and move the app files into it. having the app file within the same folder as the staticfile won't work. – fischermatte Feb 28 '18 at 08:46