Keystone-classic: Caching mechanism with nginx in front

Created on 2 Aug 2016  Â·  7Comments  Â·  Source: keystonejs/keystone-classic

I'm deploying my Keystone application 0.4 in production next week but I'm still struggling with caching images, css and js files with nginx in front of the web application.

I've been reading the documentation and I changed the environment variables with the NODE_ENV="production" to enable caching based on the documentation but it doesn't work. I aslo changed the keystone.js file without success

keystone.set('locals', {
_: require('lodash'),
//env: keystone.get('env'),
env:'production',

The nginx server hosted by Goocle Compute Engine is configured to cache css, jpg files based on this tutorial https://serversforhackers.com/nginx-caching but no files are being cached.

Steps to reproduce the behavior

curl -X GET -I localhost:3000/css/style.css

Expected behavior

curl -X GET -I localhost:3000/css/style.css
HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Cache-Control: public
Cache-Control: max-age=31536000
Expires: Tue, 02 Aug 2017 03:57:27 GMT

Last-Modified: Thu, 28 Jul 2016 03:33:46 GMT
ETag: W/"142cb-1562f90b080"
Content-Type: text/css; charset=UTF-8
Content-Length: 82635
Vary: Accept-Encoding
Date: Tue, 02 Aug 2016 03:57:27 GMT
Connection: keep-alive

Actual behavior

HTTP/1.1 200 OK
X-Powered-By: Express
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Thu, 28 Jul 2016 03:33:46 GMT
ETag: W/"142cb-1562f90b080"
Content-Type: text/css; charset=UTF-8
Content-Length: 82635
Vary: Accept-Encoding
Date: Tue, 02 Aug 2016 03:57:27 GMT
Connection: keep-alive

How can I change the directive to get rid of the Cache-Control.max-age value? Please, it's urgent.

needs input

Most helpful comment

Aha, there is a difference between compiled templates and files in your public folder. One is generated by Keystone and the other is gotten from disk.

It's not safe in general to assume that files in your public folder can be cached. However, I think that it is common enough that maybe the default could be 'static options': {maxAge: "200d"}, and if you want something else you need to provide your own.

Thoughts @JedWatson @mxstbr @webteckie?

All 7 comments

This is really a question for stack overflow, unless you are saying that there is an issue with the documentation claiming something and that not happening.

That said, take a look at https://github.com/keystonejs/keystone/blob/master/server/bindStaticMiddleware.js where it gives whatever you put in static options to the static file middleware.

Maybe I misunderstood the caching mechanism with keystonejs but I was assuming the caching was being taking care when you switch the NODE_ENV from 'development' to 'production'.

That's said, I don't mind using 'static' options but my files are already in the 'public' folder (css, home jpg page). The other one I would like to cache are the one from Cloudinary but it's not the priority.

The environment setting to use. The keys development and production are supported, and this will have an impact on things like caching compiled templates. Defaults to process.env.NODE_ENV || "development".
You should really set this to production for your production servers using the NODE_ENV environment variable. Several other modules expect this convention also.

view cache Boolean
This option is passed through to Express, and controls whether compiled view templates are cached between requests.
It defaults to true in production, or false otherwise so there is rarely any reason to set it explicitly.

One or more paths to your application's static files. Setting this will include the serve-static middleware.
If you're following the recommended project structure, this should be set to 'public'.

Thanks

Aha, there is a difference between compiled templates and files in your public folder. One is generated by Keystone and the other is gotten from disk.

It's not safe in general to assume that files in your public folder can be cached. However, I think that it is common enough that maybe the default could be 'static options': {maxAge: "200d"}, and if you want something else you need to provide your own.

Thoughts @JedWatson @mxstbr @webteckie?

Thank you for the quick response! Very much appreciated

I just did this exact thing with nginx. Reverse proxy with caching and serving the static content. Did some synthetic load testing and the nginx option was _much_ easier on the CPU. Additionally, it kept the site available if keystone was taken offline.

My nginx.conf

I think that would make a nice Medium blogpost, right @mxstbr?

On Sun, Aug 21, 2016, 3:50 AM Mark Hoffmeyer [email protected]
wrote:

I just did this exact thing with nginx. Reverse proxy with caching and
serving the static content. Did some synthetic load testing and the nginx
option was _much_ easier on the CPU. Additionally, it kept the site
available if keystone was taken offline.

My nginx.conf
https://gist.github.com/mhoffmeyerDC/3a139d612e2ea3635d0a836a0546832d

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/keystonejs/keystone/issues/3257#issuecomment-241234108,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADWloZsWqjTg5fgF3NWhAM_5hpPsTfZks5qh67pgaJpZM4JaQR5
.

Yes!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josephg picture josephg  Â·  4Comments

christroutner picture christroutner  Â·  4Comments

joernroeder picture joernroeder  Â·  5Comments

molomby picture molomby  Â·  5Comments

celiao picture celiao  Â·  4Comments