Nginx ignore query string for caching certain files

13,076

You can use proxy_cache_key. It defining a key to lookup the cache. The idea is the key shouldn't have the query string.

By default, the directive’s value is close to the string

proxy_cache_key $scheme$proxy_host$uri$is_args$args;

So you want set

proxy_cache_key $scheme$proxy_host$uri;

to force caching.

Source: nginx mailing-list

Share:
13,076

Related videos on Youtube

Andrew Sun
Author by

Andrew Sun

Maybe I shouldn't prefer to keep an air of mystery about myself.

Updated on September 18, 2022

Comments

  • Andrew Sun
    Andrew Sun almost 2 years

    I know my setup is a bit crazy, but whatever....

    I set up Nginx on Openshift to cache map tiles (for a map viewer, you can guess the purpose, :-) ) that are served from my home network, which has limited bandwidth (stupid wireless connections!). Openshift gives me unlimited bandwidth and 1 GB of disk, which should be enough to cache popular portions of the map.

    However, the map viewer likes to make requests like this:

    http://localhost/tiles/world/t/-1_0/-27_23.png?1381358434308
    

    Which makes nginx think the file is not cacheable! I've done some googling, but since I'm horrible at reading and writing regexes, I would like to request (from you) a way to make nginx ignore the query string for .png files and just serve the version from cache without the query string.

    Here are the relevant parts of the server config:

    http {
    
      proxy_cache_path  ${OPENSHIFT_RUNTIME_DIR}/cachefile levels=1:2 keys_zone=my-cache:599m max_size=700m inactive=250m;
      proxy_temp_path ${OPENSHIFT_RUNTIME_DIR}/cachefile/tmp; 
        include mime.types;
        default_type application/octet-stream;
    
        # Format for our log files
        log_format   main '$remote_addr - $remote_user [$time_local]  $status '
          '"$request" $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"';
    
        sendfile on;
        keepalive_timeout 5;
        access_log ${OPENSHIFT_LOG_DIR}/access.log;
    
        port_in_redirect off;
        server_tokens off;
    
        tcp_nopush on; # off may be better for Comet/long-poll stuff
        tcp_nodelay off; # on may be better for Comet/long-poll stuff
    
        # Enable Gzip
        gzip  on;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_min_length 1100;
        gzip_buffers     4 8k;
        gzip_proxied any;
        gzip_types
          # text/html is always compressed by HttpGzipModule
          text/css
          text/javascript
          text/xml
          text/plain
          text/x-component
          application/javascript
          application/json
          application/xml
          application/rss+xml
          font/truetype
          font/opentype
          application/vnd.ms-fontobject
          image/svg+xml;
    
        gzip_static on;
    
        gzip_proxied        expired no-cache no-store private auth;
        gzip_disable        "MSIE [1-6]\.";
        gzip_vary           on;
      server {
          listen ${OPENSHIFT_DIY_IP}:${OPENSHIFT_DIY_PORT};
          #server_name *;
        location / {
          proxy_pass http://[CENSORED];
          proxy_cache my-cache;
          proxy_cache_valid  200 302  60m;
          if ($scheme = https) {
            rewrite ^(.*)? http://$http_host$1 permanent;
            }
        }
    
      }
    }
    
    • Michael Hampton
      Michael Hampton over 10 years
      You can't fix the software? The whole point of this query string is to prevent loading a cached resource.
    • Andrew Sun
      Andrew Sun over 10 years
      @MichaelHampton I don't need things to be that up to date. I can make the caching shorter, like 2 minutes, if my tiles change that often. Also, I didn't write the map viewer. My changes to the JS would be overwritten in an update anyway.