3

I'm following this sample configuration from the Nginx docx. Reverse-proxying works fine except that it isn't caching results.

Here is my nginx.conf:

user http http;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    proxy_cache_path  /srv/http/my.site/cache  levels=1:2 keys_zone=STATIC:10m inactive=24h  max_size=1000m;
    proxy_temp_path   /srv/http/my.site/tmp;

    server {
        listen 8081;
        server_name my.site remote.host;

        location / {
            proxy_pass             http://remote.host;
            proxy_cache            STATIC;
            proxy_cache_valid      200 302 1d;
            proxy_cache_valid      404     1m;
        }
    }
}

Folder permissions are OK as far as I can tell:

ls -l /srv/http/my.site/
total 8
drwxr-xr-x 2 http http 4096 Dec 21 04:24 cache
drwxr-xr-x 2 http http 4096 Dec 21 04:24 tmp
usermynut
  • 43
  • 1
  • 3
  • How do you know it's not caching results? Nothing in that folder? Try reading this http://serverfault.com/questions/30705/how-to-set-up-nginx-as-a-caching-reverse-proxy . I wonder if the headers of the resources coming back disallow caching, I know with fastcgi caching you can tell it to ignore headers - maybe you can with proxy caching too. – Tim Jan 22 '16 at 20:58
  • @Tim Yes, nothing under `/srv/http/my.site/{cache,tmp}`. I checked [headers](https://dpaste.de/sUrY/raw) with curl -I. Regarding overriding caching headers, I think you are referring to `proxy_ignore_headers "Cache-Control" "Expires";`; I've also tried that. – usermynut Jan 23 '16 at 04:30

1 Answers1

6

I'll post this as an answer because it's easier to format. Try this

proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 
proxy_cache_valid 200 302 60h;
proxy_cache_valid any 60m;
Tim
  • 30,383
  • 6
  • 47
  • 77
  • There is a typo in the config, second line, near end: "60h;m". Otherwise, thanks! – Slava N Feb 09 '19 at 07:55
  • "By default, NGINX respects the Cache-Control headers from origin servers. It does not cache responses with Cache-Control set to Private, No-Cache, or No-Store or with Set-Cookie in the response header." https://www.nginx.com/blog/nginx-caching-guide/ (And for those using Cloudflare; note it sometimes sets Set-Cookie even on static images etc.) – Peter W Mar 14 '21 at 21:59