0

I'm not able to make use of Nginx proxy_cache capability, although my server configuration seems correctly set up according to official guidelines. Is there anything I am missing here ?

Vhost configuration:

proxy_cache_path /var/www/nginx_cache levels=1:2 keys_zone=asf:1m max_size=64m
                 inactive=60m use_temp_path=off;

server {
  listen 443 ssl;

  server_name my_domain.com;
  root /var/www/my_site;
  index index.php index.html;

  include /etc/nginx/snippets/lets_encrypt_challenge.conf;

  location / {
        try_files $uri /controller.php$is_args$args;
  }

  location ~* \.(js|css|png|jpe?g|gif|ico|woff2?|eot)$ {
     expires 7d;
     add_header Cache-Control "public, no-transform";
  }

  location ~* ^/[^/]+\.php {
      proxy_cache asf;
      proxy_cache_lock on;
      # The PHP application sets a cache-control header which disables
      # caching mechanism by default. So next directive is required to tell to ignore this header.
      proxy_ignore_headers Cache-Control; 
      add_header X-Proxy-Cache $upstream_cache_status always;

      fastcgi_split_path_info ^([^/]+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      fastcgi_index controller.php;

      include fastcgi_params;
      fastcgi_param DOCUMENT_ROOT $realpath_root;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;

      fastcgi_intercept_errors on;
      fastcgi_buffer_size 16k;
      fastcgi_buffers 4 16k;
      fastcgi_connect_timeout 180;
      fastcgi_send_timeout 180;
      fastcgi_read_timeout 180;
  }
}

Response headers bag (don't even contain the X-Proxy-Cache added on purpose to track whether cache was hit or not):

Cache-Control: public, max-age=84600
Connection: keep-alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Sun, 04 Feb 2018 18:42:18 GMT
Server: nginx
Transfer-Encoding: chunked

Thank you.

Stphane
  • 111
  • 7
  • You should to use `fastcgi_cache_path` and `fastcgi_cache` for `fastcgi_pass` – Alexey Ten Feb 05 '18 at 14:14
  • @AlexeyTen that was the problem, cache gets hit now according to the added header value "HIT". Could you turn your comment into an answer so I could accept it ? [This answer helped me too](https://serverfault.com/questions/620596/what-is-the-difference-between-proxy-cache-and-fastcgi-cache) – Stphane Feb 10 '18 at 11:41

0 Answers0