Questions about `nginx`'s `cache`
ringa_lee
ringa_lee 2017-05-16 17:24:57
0
1
552

I have a request: http://www.example.com/app?test=1 . I hope to cache this when there is the request parameter test=1 url. Can nginx do it? .

I write

in the configuration file
service {

    location /app {
      if ( $arg_test ) {
        //nginx 的 cache 的配置
      }
    }
}

But an error is reported when starting. Tells me cache cannot be configured in an if block. . .

ringa_lee
ringa_lee

ringa_lee

reply all(1)
淡淡烟草味

First of all, of course you cannot put 全部 configuration in if

But you can do this

location /app {
    proxy_set_header Host $host;
    proxy_cache_path /data/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
    if ( $arg_test ){
        proxy_pass http://backend:backport;
    }
}

This answer may not meet your requirements, but is it really necessary to conditionalize the cache configuration?
Posting a more detailed configuration file may help you understand your needs better...

Welcome to follow my segmentfault blog.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template