5

I am using the following config on my http block on nginx.conf:

fastcgi_cache_path /var/www/nginx_cache levels=1:2 keys_zone=NGINXCACHE:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

This works for all websites hosted on this nginx server. I would like to know if it is possible to have each website cache files stored under a specific folder, something like:

  • /tmpfs/fastcgi_cache/website1
  • /tmpfs/fastcgi_cache/website2

When I daclare multiple fastcgi_cache_path's nginx start gives me a error, even with different zones.

Best regards.

  • 1
    What did the error say? – Michael Hampton Sep 16 '13 at 0:18
  • nginx: [emerg] "fastcgi_cache_key" directive is duplicate in /etc/nginx/nginx.conf:38 – ddutra Sep 16 '13 at 0:23
  • 1
    Y U NO READ DOX??? / fastcgi_cache_path -> Context: http – that guy from over there Sep 16 '13 at 5:53
  • Sorry, I have read the docs if that is what it is supposed to mean. Maybe I am missing something, but I think there is no reference to keeping each website cache files under a specific folder. I have not developed purging capabilities to my websites yet, and clients rarely update them, but they do. My only resource to purge the cache is deleting all the folders content, and I would like to do that on a website basis and not delete all cache files for all websites running under this nginx. Maybe I am missing something obvious, if that is the case can you please point it out to me? – ddutra Sep 16 '13 at 11:49
  • 1
    no, you read it correct; you can have just one global fastcgi_cache_path, in opposite to proxy_cache where you can define multiple locations for multiple sites. you might find a workaround with fastcgi_store, but from the docs: To be clear fastcgi_store is not a cache, it's rather mirror on demand. – that guy from over there Sep 16 '13 at 15:31
12

You can define multiple cache paths in the nginx http context:

fastcgi_cache_path /var/run/nginx/cache/site1 levels=1:2 keys_zone=SITE1:100m inactive=1w;
fastcgi_cache_path /var/run/nginx/cache/site2 levels= keys_zone=SITE2:123m inactive=60m;
# other fastcgi_cache_* settings here or in your servers/locations

server {
    server_name site1.com;
    # blablabla

    location ~ \.bla$ {
        # blablabla
        fastcgi_cache SITE1;
    }
}

server {
    server_name site2.com;
    # blablabla

    location ~ \.bla$ {
        # blablabla
        fastcgi_cache SITE2;
    }
}

My nginx:

nginx -V
nginx version: nginx/1.1.19
TLS SNI support enabled
configure arguments:
--prefix=/etc/nginx
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-client-body-temp-path=/var/lib/nginx/body
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi
--http-log-path=/var/log/nginx/access.log
--http-proxy-temp-path=/var/lib/nginx/proxy
--http-scgi-temp-path=/var/lib/nginx/scgi
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi
--lock-path=/var/lock/nginx.lock
--pid-path=/var/run/nginx.pid
--with-debug
--with-http_addition_module
--with-http_dav_module
--with-http_geoip_module
--with-http_gzip_static_module
--with-http_image_filter_module
--with-http_realip_module
--with-http_stub_status_module
--with-http_ssl_module
--with-http_sub_module
--with-http_xslt_module
--with-ipv6
--with-sha1=/usr/include/openssl
--with-md5=/usr/include/openssl
--with-mail
--with-mail_ssl_module
--add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-auth-pam
--add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-echo
--add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-upstream-fair
--add-module=/build/buildd/nginx-1.1.19/debian/modules/nginx-dav-ext-module
3

For anybody that followed https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps and is having issues setting up caching for multiple sites and is getting the error nginx: [emerg] "fastcgi_cache_key" directive is duplicate.

My solution was to place the fastcgi_cache_key "$scheme$request_method$host$request_uri"; within /etc/nginx/nginx.conf. As opposed to putting it in /etc/nginx/sites-enabled/vhost config which the above tutorial alludes to.

Hope this helps anybody else having a similar issue to me.

  • That is exactly what the existing answer advises to do. – Michael Hampton Aug 8 '16 at 21:55

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.