Why is my nginx reverse-proxy not caching anything?

5,192

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;
Share:
5,192

Related videos on Youtube

usermynut
Author by

usermynut

Updated on September 18, 2022

Comments

  • usermynut
    usermynut almost 2 years

    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
    
    • Tim
      Tim over 8 years
      How do you know it's not caching results? Nothing in that folder? Try reading this serverfault.com/questions/30705/… . 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.
    • usermynut
      usermynut over 8 years
      @Tim Yes, nothing under /srv/http/my.site/{cache,tmp}. I checked headers with curl -I. Regarding overriding caching headers, I think you are referring to proxy_ignore_headers "Cache-Control" "Expires";; I've also tried that.
  • Slava N
    Slava N over 5 years
    There is a typo in the config, second line, near end: "60h;m". Otherwise, thanks!
  • Peter W
    Peter W over 3 years
    "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." nginx.com/blog/nginx-caching-guide (And for those using Cloudflare; note it sometimes sets Set-Cookie even on static images etc.)