1

I want to handle all [GET] requests to /api from cache and handle all other requests as in last location block (without cache). All the requests to /api with methods PUT, POST, DELETE also have not to use cache.

I saw the similar question here Nginx proxy by Request Method, but still can not understand how to use it in my case.

Thanks in advance.

My config:

location / {
    root /var/www/project/web;
    # try to serve file directly, fallback to app.php
    try_files $uri /app.php$is_args$args;
}


location ~ ^/api {
    root /var/www/project/web/app.php;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/?.*)$;
    include fastcgi_params;
    fastcgi_cache fcgi;
    fastcgi_cache_valid 200 5m;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;

}

location ~ ^/(app|app_dev|config)\.php(/|$) {
    root /var/www/project/web;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/?.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}
2
  • Which cache are you referring to, the fastcgi_cache? If GET methods only use the cache, how will the cache get updated? Feb 5, 2016 at 15:00
  • Mark Stosberg, yes, fastcgi_cache. if I understood, it has to be invalidated after TTL. "fastcgi_cache_valid 200 5m" - it means TTL = 5 minutes?
    – zIs
    Feb 5, 2016 at 17:45

1 Answer 1

0

Here my question was answered stackoverflow.com/a/35291314/1517230 by clever stackoverflow users :)

location ~ ^/api {
    root /var/www/project/web/app.php;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/?.*)$;
    include fastcgi_params;

    # Don't cache anything by default
    set $no_cache 1;

    # Cache GET requests
    if ($request_method = GET)
    {
        set $no_cache 0;
    }

    fastcgi_cache_bypass $no_cache;
    fastcgi_no_cache $no_cache;

    fastcgi_cache fcgi;
    fastcgi_cache_valid 200 5m;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}

or

map $request_method $api_cache_bypass {
    default       1;
    GET           0;
}

location ~ ^/api {
    root /var/www/project/web/app.php;
    fastcgi_send_timeout 600s;
    fastcgi_read_timeout 600s;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/?.*)$;
    include fastcgi_params;

    fastcgi_cache_bypass $api_cache_bypass;
    fastcgi_no_cache $api_cache_bypass;

    fastcgi_cache fcgi;
    fastcgi_cache_valid 200 5m;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .