4

If I make a request to a Nginx-proxy server configured to cache an upstream I get the following response headers:

{'content-length': '13200000', 'x-cache-status': 'MISS', 'server': 'nginx/1.9.9', 'connection': 'keep-alive', 'cache-control': 'max-age=45', 'date': 'Fri, 27 Jan 2017 10:57:55 GMT'}

a couple of seconds later I do the same request again, I get the following headers:

{'content-length': '13200000', 'x-cache-status': 'HIT', 'server': 'nginx/1.9.9', 'connection': 'keep-alive', 'cache-control': 'max-age=45', 'date': 'Fri, 27 Jan 2017 10:58:18 GMT'}

The upstream-server specifies the max-age header to 45 seconds, shouldn't the second response header have an updated max-age header? That is max-age=45-(time between the requests)?

Edit

An example config reproducing the behaviour:

http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream backend {
        server localhost:8080;
    }

    proxy_cache_path /etc/nginx/wwwroot/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen 80;

        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            add_header X-Cache-Status $upstream_cache_status;
        }
    }

    server {
        listen 8080;
        root /etc/nginx/wwwroot;
        expires 60s;
    }
}

Result:

root@ubuntu:/home/parallels# curl -I localhost/testfile.txt
HTTP/1.1 200 OK
Server: nginx/1.10.0 (Ubuntu)
Date: Sat, 28 Jan 2017 19:08:45 GMT
Content-Type: text/plain
Content-Length: 12
Connection: keep-alive
Last-Modified: Fri, 27 Jan 2017 14:45:39 GMT
ETag: "588b5d13-c"
Expires: Sat, 28 Jan 2017 19:09:45 GMT
Cache-Control: max-age=60
X-Cache-Status: MISS
Accept-Ranges: bytes

root@ubuntu:/home/parallels# curl -I localhost/testfile.txt
HTTP/1.1 200 OK
Server: nginx/1.10.0 (Ubuntu)
Date: Sat, 28 Jan 2017 19:08:48 GMT
Content-Type: text/plain
Content-Length: 12
Connection: keep-alive
Last-Modified: Fri, 27 Jan 2017 14:45:39 GMT
ETag: "588b5d13-c"
Expires: Sat, 28 Jan 2017 19:09:45 GMT
Cache-Control: max-age=60
X-Cache-Status: HIT
Accept-Ranges: bytes
3
  • Interesting question. Can you please edit your question to share the Nginx server config? – Tim Jan 27 '17 at 18:55
  • Updated the question with example config and result – olif Jan 28 '17 at 19:15
  • @olif Hi, did you find a solution to this? – Vixxs Dec 20 '19 at 23:36
0

In your configuration Nginx is simply a reverse proxy. It isn't rewriting anything as you haven't told it to rewrite anything. It's simply storing pages generated by the upstream server and delivering them when it's told to.

The upstream server has set the expires time, there's also a cache-control with a max-age in seconds. I suspect Nginx doesn't read these directives and keeps things in its cache as long as you've told it to, but someone else might correct me here.

I don't think there's any way to have Nginx keep track of when the upstream server sent a page and rewrite the headers of the page before delivering it to the client. This would have to be done in the app server.

Expires is http/1.0, cache-control is http/1.1, according to this article. You can use either, but they conflict, so I guess browsers will refresh the page when either tell it to.

IMHO you need to work out what you want, then configure your application server or Nginx to use it. Nginx can rewrite headers if you have the headers_mod module either from your distribution or compiled in. If there's a time that the page must not be cached after I suggest you use Expires. If you just want browsers to have reasonably fresh pages then cache-control is better as you can define things like caching by proxy servers and CDNs. You can always have Expires specify the time and cache-control specify the CDN and proxy cache behavior, including revalidation.

Suggest more thought and research and you'll work out the rest on your own. Useful reference about cache-control here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.