rob
rob

Reputation: 61

Nginx - "force" 200 cache response instead of 304

Is it possible somehow to force a 200 (cache) response instead of a 304 not modified for static files? If so you could potentially save a lot of server requests and speed up the loadtime considerbly for returning visitors.

Upvotes: 6

Views: 6368

Answers (3)

Rick O'Shea
Rick O'Shea

Reputation: 1449

200 is not a cache response; it indicates success and it requires the response to contain the result. 304 indicates an unmodified resource; that is to say you have it already, so nothing is returned. So, what you are suggesting should cause all 304's masquerading as 200's to fail. Even if it doesn't your not going to save any requests or speed anything up with fake 200 responses.

EDIT: 200 in some cases means the resource has been taken from cache with success...

Upvotes: 0

Ryan Thompson
Ryan Thompson

Reputation: 488

I've found Expires alone does not properly set the behavior. Below has been a sure fire:

# Set cache
expires 1M;
add_header Pragma public;
add_header Cache-Control "public";

This going in your location block desired..

I also think there is a lot of misleading information out there on this.. That 200 is somehow the same as 200 (cache).

From my understanding:

200 - server request - transfer OK 200 (cache) - OK - no server request (from cache) 304 - server request - no transfer (not modified)

200 (cache) as I understand it does NOT make a server request at all.

Upvotes: 4

Mark Rose
Mark Rose

Reputation: 981

Set the expires directive in Nginx in the locations you server your static files from. Browsers will not bother checking back with the server until the expires time is reached. At that point, the server may still send back a 304 response if the static file hasn't changed and the expires time will have to be reached before the browsers check again.

By the way, the 200 response code simply means the requested resource is being returned. It has nothing to do with whether any caching headers are sent with the HTTP response or not.

Upvotes: 1

Related Questions