nginx-proxy: Can't get proxy_cache to work

Hi,

I think I’m having trouble with nginx proxy_cache settings and nginx-proxy. I’m not sure if you want questions here so if you feel this would better be served asking elsewhere please let me know.

I’m using nginx-proxy to reverse proxy to a node application running in a separate docker container on the same host.

I’ve got a proxy configuration file proxy.conf:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=one:10m max_size=10g;

and a file for the virtual host specifying how the reverse proxy should cache responses my.virtual.host_location:

proxy_cache one;
add_header X-Cached $upstream_cache_status;

The X-Cached header always has MISS.

When I set nginx up manually and exposed the port on the node app I was able to get HITs. I assume I’m doing something incorrectly in my configuration but I really don’t know how to tell why a request is a cache hit or not.

Here’s a sample set of response headers:

Access-Control-Allow-Origin → *
Cache-Control → max-age=12000
Connection → keep-alive
Content-Length → 189
Content-Type → application/json; charset=utf-8
Date → Thu, 24 Sep 2015 04:49:49 GMT
ETag → W/"bd-aoxcrTy+QadpC3n9/+drVw"
Server → nginx/1.9.2
Vary → Accept-Encoding
X-Cached → MISS
X-Powered-By → Express

And here’s my docker-compose.yml

nginx:
  restart: always
  image: jwilder/nginx-proxy:latest
  ports:
    - 80:80
  volumes:
    - /var/run/docker.sock:/tmp/docker.sock:ro
    - /etc/my-path/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
    - /etc/my-path/vhost.d:/etc/nginx/vhost.d:ro

api:
  restart: always
  image: docker.my.domain:5000/image:4.0.0
  environment:
    VIRTUAL_HOST: my.virtual.host
    NODE_ENV: prod

I only just found out about the _location vhost files, I was originally trying with the original system and I had the same problems then too.

Thanks for your time.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (4 by maintainers)

Commits related to this issue

Most upvoted comments

For the proxy_cache to work, proxy_buffering must be set to on. See https://forum.nginx.org/read.php?2,264946,265001#msg-265001

But nginx.tmpl contains proxy_buffering off: https://github.com/jwilder/nginx-proxy/blob/3fab237f349d30b7cceb0e95a1b27418f89a2f8c/nginx.tmpl#L72

So to make caching work, you should set proxy_buffering on near the proxy_cache directive.

Note, however, that you might start getting warnings like this:

[warn] 7#7: *1 an upstream response is buffered to a temporary file

Which can be fixed by proxy_max_temp_file_size 0

proxy_cache_valid any 48h; solved it for me

proxy_cache_valid did the job 👍

Thought I’d chip in my solution.

Mount the volumes of the files:

  volumes:
      - ./nginx/proxy.conf:/etc/nginx/conf.d/proxy.conf:ro
      - ./nginx/vhost.d:/etc/nginx/vhost.d:ro

Setup the proxy file in ./nginx/proxy.conf

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

Setup the location file for the server block ./nginx/vhost.d/youtech.localhost_location . Be sure to ignore the headers your server sends back, otherwise it won’t cache.

proxy_cache my_cache;
proxy_buffering on;
proxy_ignore_headers "Cache-Control" "Expires";
proxy_cache_valid any 48h;

Finally set up the cache status file ./nginx/vhost.d/youtech.localhost . Delete this file once tested and in production.

add_header X-Cached $upstream_cache_status;

I’m having the same problem as @Bockit.

Have you found a solution?

I’m guessing it’s a permissions problem. It looks like the base nginx container creates a /var/cache/nginx volume, but it stays owned by root. I suspect it needs to be writable by www-data.