Nginx 忽略用于缓存某些文件的查询字符串

And*_*Sun 7 nginx cache reverse-proxy querystring

我知道我的设置有点疯狂,但无论如何......

我在 Openshift 上设置了 Nginx 来缓存地图图块(对于地图查看器,您可以猜到目的,:-)),这些图块由我的家庭网络提供,该网络带宽有限(愚蠢的无线连接!)。Openshift 为我提供了无限带宽和 1 GB 磁盘,这应该足以缓存地图的热门部分。

但是,地图查看器喜欢发出这样的请求:

http://localhost/tiles/world/t/-1_0/-27_23.png?1381358434308
Run Code Online (Sandbox Code Playgroud)

这让 nginx 认为该文件不可缓存!我已经做了一些谷歌搜索,但由于我在阅读和编写正则表达式方面很糟糕,我想(从你那里)请求一种方法让 nginx 忽略 .png 文件的查询字符串,并且只从缓存中提供版本而不需要请求参数。

以下是服务器配置的相关部分:

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

  }
}
Run Code Online (Sandbox Code Playgroud)

mas*_*oeh 11

您可以使用proxy_cache_key. 它定义了一个键来查找缓存。这个想法是关键不应该有查询字符串。

默认情况下,指令的值接近字符串

proxy_cache_key $scheme$proxy_host$uri$is_args$args;
Run Code Online (Sandbox Code Playgroud)

所以你想设置

proxy_cache_key $scheme$proxy_host$uri;
Run Code Online (Sandbox Code Playgroud)

强制缓存。

来源:nginx 邮件列表