Nginx – Does Nginx `if_modified_since` only work on static resources

cachehttp-headersnginx

I can't seem to find enough documentation. I have an app that generates some dynamic responses, but could still benefit from the Last-Modified header — so I send it.

However, turning on if_modified_since (set to before, per http://nginx.org/en/docs/http/ngx_http_core_module.html#if_modified_since) doesn't seem to have any effect on non-static resources. E.g., php, python apps.

Is this because Nginx isn't just looking at my response Last-Modified header? Because I can see that they appear to be set correctly, as below:

> GET /3.0/view.json?id=2 HTTP/1.1
> Host: xxxxxxxxxxxxx
> Accept: */*
> If-Modified-Since: Sat, 02 May 2015 19:43:02 GMT
>
< HTTP/1.1 200 OK
* Server nginx/1.4.7 is not blacklisted
< Server: nginx/1.4.7
< Date: Fri, 01 May 2015 19:56:05 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< Last-Modified: Fri, 01 May 2015 19:56:05 GMT

Or is there something larger that I'm overlooking? Just curious how
if_modified_since is implemented, compared to where I'm setting my expectations. I assumed it would just look at the response headers, and over-ride the status as necessary. Am I wrong?

Best Answer

Sending Last-Modified headers in your app replies is a start but it seems you don't handle If-Modified-Since properly on incoming requests because your app should reply 304 Not Modified and not 200 OK. Changing the directive on nginx only impact requests served directly by nginx i.e. static ressources unless you configure it as a reverse proxy cache. In this case, you may serve stale replies regarding this header value since content will be cached for a period of time without hitting your app. Turning <X>_cache_revalidate on will use the If-Modified-Since header to revalidate cache content between nginx's cache and your app once it has expired (where <X> = proxy / fastcgi / scgi / uwsgi)