search

Home  >  Q&A  >  body text

Questions about `nginx`’s `cache`

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_leeringa_lee2777 days ago574

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 17:26:57

    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.

    reply
    0
  • Cancelreply