S
S
Stilar2018-08-28 06:43:49
Nginx
Stilar, 2018-08-28 06:43:49

nginx directory caching exception?

there is a subdomain for returning api (json or xml )
most of the data is static, given without authorization, and their obsolescence is not critical, therefore everything is cached using nginx tools,
but there are requests (directories) where data should be returned without cache (or with a minimum cache - 1m )
config:

proxy_cache_path /mnt/cache_nginx/json levels=1:2 keys_zone=cache_api:100m inactive=72h max_size=1G;

server {
        listen 443 ssl;
        location ~ (\.xml|\.json)$ {
              proxy_cache             cache_api;
              proxy_cache_valid       502 503 1m;
              proxy_cache_valid       404 1h;
              proxy_cache_valid       any 3d;
              proxy_cache_key         $request_uri;

              proxy_intercept_errors off;
              proxy_read_timeout 30s;
              proxy_send_timeout 30s;
        }

        # зона запросов, где кеш отключен
        location /user/ {
            proxy_cache off;
        }
}

According to my logic, urls to the /user/* zone should be given without caching. But it's not. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danila Vershinin, 2018-08-28
@Stilar

First, nginx checks the location with the prefix, then it finds the location with the regex since it is more specific. Accordingly, all directives will be applied from `location ~ (\.xml|\.json)$ {`.
The solution is to make /user/ more specific and put it at the beginning (order is important):

proxy_cache_path /mnt/cache_nginx/json levels=1:2 keys_zone=cache_api:100m inactive=72h max_size=1G;

server {
        listen 443 ssl;

        # зона запросов, где кеш отключен
        location ~ /user/.*(\.xml|\.json)$ {
              proxy_cache             off;

              proxy_intercept_errors off;
              proxy_read_timeout 30s;
              proxy_send_timeout 30s;
        }

        location ~ (\.xml|\.json)$ {
              proxy_cache             cache_api;
              proxy_cache_valid       502 503 1m;
              proxy_cache_valid       404 1h;
              proxy_cache_valid       any 3d;
              proxy_cache_key         $request_uri;

              proxy_intercept_errors off;
              proxy_read_timeout 30s;
              proxy_send_timeout 30s;
        }


}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question