Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: enable/disable caching depending on HTTP method

Tags:

nginx

I asked this question on serverfault, but nobody answered. Hope, stackoverflow people know Nginx better :)

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, 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;
}
like image 206
zIs Avatar asked Feb 09 '16 11:02

zIs


People also ask

How do I enable NGINX caching?

Go to the “Web Server” tab. In the “nginx settings” section, select the “Enable nginx caching” checkbox. (Optional) You can customize nginx caching settings. If you are not familiar with nginx caching, we recommend that you keep the default settings.

How do you cache static resources using HTTP caching NGINX?

Point your browser to the newly configured NGINX server and open up a static file such as a JPG image. What you should see in the HTTP Header Live sidebar is an Expires header and a Cache-Control header with a max-age directive (Figure B). That's all there is to enabling static content caching in NGINX.

Can NGINX cache dynamic content?

Cache both static and dynamic content from your proxied web and application servers, to speed delivery to clients and reduce the load on the servers.


1 Answers

It's pretty simple thankfully. Nginx's modules (proxy, fastcgi, uwsgi etc) all have the ability to inform a request not to use the cache.

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;
}

As per Richard Smith's suggestion, a more elegant solution using the maps directive is below:

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;
}

The additions to the location are essentially telling Nginx to use or ignore the cache depending on the verb. It sets $no_cache to 1, which will bypass the cache for all requests, except where the method is GET when it is set to 0 which instructs it to use the cache (if available).

like image 105
justcompile Avatar answered Oct 24 '22 03:10

justcompile