Nginx not caching any proxied requests

redburn asked:

I believe I’ve properly set up nginx to cache all requests proxied to PHP-FPM, but it’s not working.

Here’s the “http” bit from /etc/nginx/nginx.conf:

http {
     include       /etc/nginx/mime.types;
     default_type  application/octet-stream;

     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  /var/log/nginx/access.log  main;

     sendfile        on;

     proxy_cache_path        /var/cache/nginx/static levels=1:2 keys_zone=static:20m;

     include /etc/nginx/conf.d/*.conf;

     keepalive_timeout   30;

     fastcgi_buffers     8 16k;
     fastcgi_buffer_size 32k;

     gzip                on;
     gzip_types          text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
     gzip_comp_level     5;
     gzip_vary           on;
     gzip_proxied        any;
     gzip_http_version   1.0;

     server_tokens       off;
     etag                off;

}

And in my “server” bit:

location ~* .php$ {

        proxy_cache             static;
        proxy_cache_key         "$host$request_uri";
        proxy_cache_valid       any 30m;

        try_files               $uri =404;
        fastcgi_pass            127.0.0.1:9000;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param           PATH_INFO $fastcgi_script_name;
        fastcgi_read_timeout    600s;
        include                 /etc/nginx/fastcgi_params;

}

After requesting any .php file on the domain, nothing gets cached in /var/cache/nginx/static. No cookies are set, the cache directory’s writeable by nginx, and nothing unusual shows in nginx’s error log.

Any ideas? This is nginx/1.7.1 on a CentOS 6.5 VM.

My answer:


You are using FastCGI, not proxy. So you need to change all the proxy_* requests to their equivalent fastcgi_* directives. E.g. fastcgi_cache instead of proxy_cache, and so on, for all of them.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.