Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx prevent loading from cache

Tags:

php

nginx

I am updating my site frequently after finishing updates my clients reporting that old images & scripts are getting loaded instead of new ones. I know they are coming from their browser cache but is there any way i can force scripts not to load from cache in server.

I am using nginx with php-fpm.

like image 619
Ramesh Paul Avatar asked Apr 05 '13 16:04

Ramesh Paul


People also ask

Does nginx cache by default?

By default, NGINX Plus caches all responses to requests made with the HTTP GET and HEAD methods the first time such responses are received from a proxied server. As the key (identifier) for a request, NGINX Plus uses the request string.

How do I enable nginx caching?

Go to the “Web Server” tab. In the “nginx settings” section, select the “Enable nginx caching” checkbox. (Optional) You can customize nginx caching settings. If you are not familiar with nginx caching, we recommend that you keep the default settings.

How do I add cache control headers in nginx?

The header module is a core Nginx module, which means it doesn't need to be installed separately to be used. To add the header module, open the default server block Nginx configuration file in vi (here's a short introduction to vi ) or your favorite text editor: sudo vi /etc/nginx/nginx. conf.


1 Answers

You can force HTTP headers to influence the browser caching behavior, however this is probably not a good idea in a production environment where you want caching.

So simply use something like:

expires -1

To force Cache-Control no-cache header

Check here for more information:

http://wiki.nginx.org/HttpHeadersModule

That being said, I have gotten myself in the habit of just changing image and static files names as I revise them. Perhaps this comes from working with CDN's where this can be incredibly helpful. So say I have static files that I might update often (i.e. they are not part of some specific piece of content). I would name them like:

someimagev1.jpg
someimagev2.jpg
somejs1.js
somejs2.js
etc.

I change values (and links in HTML source) as needed.

like image 192
Mike Brant Avatar answered Oct 20 '22 07:10

Mike Brant