栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx 缓存服务器

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx 缓存服务器

proxy_cache_key过滤请求参数

为一个响应较慢的服务设置缓存,但是应用请求统一规范要求以timestamp作为参数。因为缓存以请求路径加参数/api/path?param=value¶m1=value来作为缓存键的,因此这样的话和没有缓存有任何区别。

要过滤timestamp参数

http {
    # Create a storage location for 
    # caching and call this location "my-app"
    proxy_cache_path /data/nginx/cache levels=1:2 
         keys_zone=myapp:10m 
         max_size=100m inactive=60m;
    
    server {
    
        location /api/  {
            # Strip out query param "timestamp"
            if ($args ~ (.*)×tamp=[^&]*(.*)) {
                set $args $1$2;
            }

            # Use the "my-app" cache defined above
            proxy_cache my-app;

            # Only cache 200 responses and cache for 20mins
            proxy_cache_valid 200 20m;

            # Create a unique cache key
            proxy_cache_key 
                "$scheme$request_method$host$uri$is_args$args";
            
            # Proxy the request
            proxy_pass http://myapi.com;
        }
}
nginx proxy_cache_path 缓存配置解释

使用proxy_cache_path指令定义共享内存缓存区域和缓存的文件系统路径。

proxy_cache_path /var/nginx/cache 
                 keys_zone=CACHE:60m 
                 levels=1:2 
                 inactive=3h 
                 max_size=20g;
proxy_cache CACHE;

上面例子:

  • 指定创建一个文件系统路径: /var/nginx/cache作为缓存目录。

  • level=1:2 定义在/var/nginx/cache创建二级目录,因为访问一个很多文件的目录速度是比较慢的

  • keys_zone创建60MB的共享内存命名为CACHE, 创建一个60MB的内存空间用于缓存key, 元数据,使用情况等。将key放在内存可以快速定位key是否存在。1MB大概可以存储8000个key.

  • inactive定义超过3h没有被访问的缓存将会被释放

  • max_size所有缓存尺寸大小不超过20G.

强制更新某个请求缓存

当HTTP出现某个header时,对某个缓存强制刷新,即使缓存没有过期。

http {
    # ...
    # 使得自定义带下划线的header生效,如
    underscores_in_headers on;

在location作用域:

location / {
  # 当请求头出现 x_refresh_cache,绕过缓存,直接访问upstrem, 收到响应后更新缓存。
  proxy_cache_bypass $http_x_refresh_cache;

性能调优
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g 
                 inactive=60m use_temp_path=off;

server {
    # ...
    location / {
        proxy_cache my_cache;
        proxy_cache_revalidate on;
        proxy_cache_min_uses 3;
        proxy_cache_use_stale error timeout updating http_500 http_502
                              http_503 http_504;
        proxy_cache_background_update on;
        proxy_cache_lock on;

        proxy_pass http://my_upstream;
    }
}
  • proxy_cache_revalidate 指导nginx从源服务器刷新内容时,发送条件性的GET请求。也就是当客户端访问过期缓存时,NGINX会附带一个If-Modified-Since请求头在发往源服务器的请求里。这样节省带宽,因为服务器只有在内容发生改动时才会返回数据。

  • proxy_cache_min_uses 设置缓存项最少使用次数,即一个缓存项在被缓存前,必须被客户端访问几次。这样可以保证经常被访问到的数据被缓存

  • 更新参数proxy_cache_use_stale和proxy_cache_background_update一般组合使用,当客户端请求到过期缓存时,nginx正在更新缓存,会暂时返回过期文件。

  • proxy_cache_lock 是多个客户端发送完全相同请求的场景下,即如果多个请求所请求内容属于同一缓存key, 这个参数启用的话,
    nginx只会往服务器发送一个请求。否则所有请求,都发往upstream服务器。

Reference List
  1. https://www.nginx.com/blog/nginx-caching-guide/
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号