I am having trouble applying object caching in Nginx, based on this article (which I visited after failing to understand the official Nginx documentation on this).

I tried to add this to the very end of the http{} block in nginx.conf:

## Nginx FastCGI Cache
    fastcgi_cache_path /var/cache/nginx/fastcgi_temp/cache levels=1:2 keys_zone=czone:100m inactive=60m;
    fastcgi_cache_key $scheme$request_method$host$request_uri;
    fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 200 301 302 60m;
    fastcgi_pass_header Set-Cookie:Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

After adding it I saved and executed nginx -t but I got an error "test failed".

The Nginx error log error gives this:

mkdir() "/var/cache/nginx/fastcgi_temp/cache" failed (2: No such file or directory)`.

Seems that the the PHP mkdir() failed.

My question

What are the correct POSIX directives for /var/ so the PHP function could create /var/cache/nginx/fastcgi_temp/ peacefully? What should be safe?

Or I shouldn't change /var/s POSIX directives and create /var/cache/nginx/fastcgi_temp/ and change only its permissions, but to what?

  • You tell us why the test failed. We can't read it off your screen! – Michael Hampton Apr 10 '18 at 20:38
  • 1
    Comment by Thomas Ward: The proper answer is "You don't give any directives to PHP, you pre-create the directory yourself and then give whatever user PHP runs as read/write in the directory (or user/group ownership). – user9303970 Apr 12 '18 at 8:23
  • In my case, that user is www-data. – user9303970 Apr 12 '18 at 9:12
  • Related: serverfault.com/questions/907338/… – user9303970 Apr 12 '18 at 12:08

As someone pointed in comments, you have to pre create the directory for the nginx cache, likely under /var/cache/nginx and give correct permissions.

I usually use to

mkdir /var/cache/nginx
chmod 755 /var/cache/nginx
chown nginx:nginx /var/cache/nginx

owner depends on your setup and distribution.

Nginx create the last directory, on my servers is chown nginx:nignx and chmod 700

  • it should be 700 writable by the nginx process. It should be www-data:www-data in your setup if nginx runs as www-data. – Federico Galli Apr 12 '18 at 10:23
  • Just to clarify, the end dir I got is /var/cache/nginx/fastcgi_temp/cache/ there seems to be a duplication. If I understand correct, I should delete this and start from scratch with the code you exampled. – user9303970 Apr 12 '18 at 10:52
  • Yes, it's not necessary but it would be simpler for you. I usually specify the app name. Example, not "cache" but "application_name".. like.. "mysite" and I end up with having /var/cache/nginx/mysite1 /var/cache/nginx/mysite2 depending on how many different caches you have – Federico Galli Apr 12 '18 at 12:18

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.