2

How to avoid url with fastcgi_cache?
no need cache list e.g

projectdomain.com/a
projectdomain.com/b
projectdomain.com/c/d

vi /etc/nginx/nginx.conf

http {
  fastcgi_cache_path /var/cache/fastcgi/projectdomain.com levels=1:2 keys_zone=projectdomain.com:10m inactive=5m;
  add_header X-Fastcgi-Cache $upstream_cache_status;

  ...

vi /etc/nginx/conf.d/default.conf

map $request_uri $dont_cache_uri {
    default 0;
    projectdomain.com/a      1;
    projectdomain.com/b      1;
    projectdomain.com/c/d    1;
}


server {
  listen 80;
  server_name projectdomain.com www.projectdomain.com;
  access_log /var/log/nginx/projectdomain.com.access.log;
  root /var/www/html/projectdomain.com;
  index index.php index.html index.htm;
  try_files $uri $uri/ /index.php?$query_string;
  client_max_body_size 1G;

  location ~ ^/sitemap/(.*)$ {
    root /var/www/html/projectdomain.com/app/Sitemap/SitemapGz;
  }
  location /robots.txt {
    alias /var/www/html/projectdomain.com/app/robots.txt;
  }
  location ~ ^/(android-chrome-36x36.png|android-chrome-48x48.png|android-chrome-72x72.png|android-chrome-96x96.png|android-chrome-144x144.png|android-chrome-192x192.png|apple-touch-icon-57x57.png|apple-touch-icon-60x60.png|apple-touch-icon-72x72.png|apple-touch-icon-76x76.png|apple-touch-icon-114x114.png|apple-touch-icon-120x120.png|apple-touch-icon-144x144.png|apple-touch-icon-152x152.png|apple-touch-icon-180x180.png|apple-touch-icon-precomposed.png|apple-touch-icon.png|browserconfig.xml|favicon-16x16.png|favicon-32x32.png|favicon-96x96.png|favicon.ico|manifest.json|mstile-70x70.png|mstile-144x144.png|mstile-150x150.png|mstile-310x150.png|mstile-310x310.png|safari-pinned-tab.svg) {
    root /var/www/html/projectdomain.com/app/favicons;
  }
  location ~ ^/(images/|javascripts/|stylesheets/|fonts) {
    root /var/www/html/projectdomain.com/app/assets;
    access_log off;
    expires max;
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 32k;
    fastcgi_buffers 8 32k;

    # cache
    fastcgi_cache projectdomain.com;
    fastcgi_cache_valid 200 60m;
    fastcgi_cache_methods GET HEAD;
    fastcgi_cache_key $scheme$request_method$host$request_uri;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    fastcgi_cache_bypass $dont_cache_uri;
    fastcgi_no_cache $dont_cache_uri;
  }
}
user1575921
  • 143
  • 8

1 Answers1

2

To avoid caching a request, you should use fastcgi_no_cache and fastcgi_cache_bypass together. The first one tells nginx not to cache a response, and the second one tells nginx to not try to find the document in the cache.

For instance, to avoid caching any request with a query string:

fastcgi_cache_bypass $is_args;
fastcgi_no_cache $is_args;

To avoid caching a request with a cookie named "logged_in_user":

fastcgi_cache_bypass $cookie_logged_in_user;
fastcgi_no_cache $cookie_logged_in_user;

To avoid caching specific paths, though, you need to combine this with a map, which lists the URLs you do not want to cache. Note that map must appear outside every server block, and inside the http block.

map $request_uri $dont_cache_uri {
    default 0;
    /a      1;
    /b      1;
    /c/d    1;
}

Then you can avoid caching all of the above.

fastcgi_cache_bypass $is_args $cookie_logged_in_user $dont_cache_uri;
fastcgi_no_cache $is_args $cookie_logged_in_user $dont_cache_uri;
Pothi Kalimuthu
  • 6,117
  • 2
  • 26
  • 38
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thanks for reply! I update code in my question, am I miss something? and what is `default` in map block and what is `0, 1` meaning? – user1575921 Nov 07 '15 at 22:37
  • 1
    @user1575921 The fastcgi directives go with all the rest of them. For the rest, you should read the documentation which is linked in blue text. – Michael Hampton Nov 07 '15 at 22:40