微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我知道我的设置有点疯狂,但不管……

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

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

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

这使得Nginx认为该文件不可缓存!我做了一些google搜索,但因为我在阅读和写作的正则表达式太可怕了,我想请求(你)的方式,使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;
        }
    }

  }
}
最佳答案
您可以使用proxy_cache_key.它定义了一个查找缓存的键.这个想法是关键不应该有查询字符串.

By default,指令的值接近字符串

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

所以你想要设置

proxy_cache_key $scheme$proxy_host$uri;

强制缓存.

资料来源:nginx mailing-list

原文地址:https://www.jb51.cc/nginx/435426.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐