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;
}
  • Which cache are you referring to, the fastcgi_cache? If GET methods only use the cache, how will the cache get updated? – Mark Stosberg Feb 5 '16 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 '16 at 17:45
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;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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