Caching POST response with nginx: should I forward the Cache-Control header to the client?

Our system uses POST requests to preload a list of assets. Given the same list of asset IDs, the server will respond with the same list of asset data. Since the list of identifiers can be very long (it is actually a multipage request containing a list of JSON), we used POST instead of GET, although it is idempotent.

We are using NGINX as a reverse proxy in front of this server. I have successfully set it up to work, but something "feels" wrong there; I am returning a header Cache-Control: max-age=3600

in POST responses that I want to cache, and I have NGINX to return them to the client.

RFC 7234 says that only the method and uri will be used as the cache key; I could use the Vary header , but it seems to be limited to other headers ...

I'm not sure how reliable the browser will be. It "seems" that if I make the HTTP POST response cacheable, it will be cached for "future GET requests", which is NOT what I intend.

So my choice looks like this:

  • Returns a header Cache-Control

    knowing (or hoping?) That there will be a reverse proxy in front of it, removing this header.
  • Bring back the title Cache-Control

    and skip it. If someone can explain why it's really reliable, that would be easy (or if there is another similar title?)
  • Don't use Cache-Control

    and instead "hardcode" all urls directly in my NGINX config (until I was able to get this working)

Is there a reliable approach I can use to achieve what I need here? Many thanks for your help.

Here's an excerpt from NGINX config in case that helps anyone:

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

location /path/to/post/request {
  proxy_pass http://remote-server;
  proxy_cache my_cache;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header proxy_http_version 1.1;
  proxy_set_header Connection "";
  proxy_cache_lock on;
  proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
  proxy_cache_methods POST;
  proxy_cache_key "$scheme$proxy_host$uri$is_args$args|$request_body";
  proxy_cache_valid 5m;
  client_max_body_size 1500k;
  proxy_hide_header Cache-Control;
}

      

+3


source to share





All Articles