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  ;
        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.


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.

  • Argh! It had to be something silly like that. Now it's working, of course. Thanks! – redburn Jul 26 '14 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.