跳转至

selective-cache-purge: NGINX 选择性缓存清除模块

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7、8、9 和 10
  • CentOS 7、8、9
  • AlmaLinux 8、9
  • Rocky Linux 8、9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-selective-cache-purge
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-selective-cache-purge

通过在 /etc/nginx/nginx.conf 顶部添加以下内容来启用该模块:

load_module modules/ngx_selective_cache_purge_module.so;

本文档描述了 nginx-module-selective-cache-purge v0.8.0,于 2018 年 3 月 26 日发布。


一个通过 GLOB 模式清除缓存的模块。支持的模式与 Redis 支持的模式相同。

配置

示例:

pid         logs/nginx.pid;
error_log   logs/nginx-main_error.log debug;

# 开发模式
# master_process      off;
# daemon              off;
worker_processes    1;
worker_rlimit_core  500M;
working_directory /tmp;
debug_points abort;

events {
    worker_connections  1024;
    #use                 kqueue; # MacOS
    use                 epoll; # Linux
}

http {
    default_type    application/octet-stream;

    access_log      logs/nginx-http_access.log;
    error_log       logs/nginx-http_error.log;

    proxy_cache_path /tmp/cache_zone levels=1:2 keys_zone=zone:10m inactive=10d max_size=100m;
    proxy_cache_path /tmp/cache_other_zone levels=1:2 keys_zone=other_zone:1m inactive=1d max_size=10m;

    #selective_cache_purge_redis_unix_socket "/tmp/redis.sock";
    #
    # 或者
    #
    #selective_cache_purge_redis_host "localhost";
    #selective_cache_purge_redis_port 6379;

    selective_cache_purge_redis_database 1;

    server {
        listen          8080;
        server_name     localhost;

        # 按前缀清除
        location ~ /purge(.*) {
            selective_cache_purge_query "$1*";
        }

        location / {
            proxy_pass http://localhost:8081;

            proxy_cache zone;
            proxy_cache_key "$uri";
            proxy_cache_valid 200 1m;
        }
    }

    server {
        listen          8090;
        server_name     localhost;

        # 按扩展名清除
        location ~ /purge/.*(\..*)$ {
            #按扩展名清除
            selective_cache_purge_query "*$1";
        }

        location / {
            proxy_pass http://localhost:8081;

            proxy_cache other_zone;
            proxy_cache_key "$uri";
            proxy_cache_valid 200 1m;
        }
    }

    server {
        listen          8081;
        server_name     localhost;

        location / {
            return 200 "请求的 URL: $uri\n";
        }
    }
}

安装说明

此模块需要: - Redis 2.8 或更高版本。使用您喜欢的包管理器安装 - apt-get、yum、brew - 或下载 Redis 并进行编译。 - hiredis 0.11.0。使用您喜欢的包管理器安装 - apt-get、yum、brew - 或下载 hiredis 并进行编译。 - redis_nginx_adapter

下载 Nginx 稳定版 源代码并解压缩。然后,您必须像往常一样运行 ./configure,并使用 --add-module 指向此项目,引用最新的 hiredis/redis_nginx_adapter 库和包含文件(如果它们不在您的默认库和包含文件夹中)。类似于以下内容:

$ ./configure \
    --with-ld-opt='-L/usr/lib/ ' \
    --with-cc-opt='-I/usr/include/hiredis/ ' \
    --add-module=/path/to/nginx-selective-cache-purge-module
$ make
$ make install

运行测试

该项目在测试套件中使用 nginx_test_helper。因此,在安装模块后,您可以直接安装所需的 gems:

$ bundle install --gemfile=test/Gemfile

并运行 rspec,指向您的 Nginx 二进制文件所在的位置(默认:/usr/local/nginx/sbin/nginx):

$ NGINX_EXEC=/path/to/nginx rspec test/

更新日志

这仍在进行中。成为改变的一部分。并查看 更新日志

GitHub

您可以在 nginx-module-selective-cache-purge 的 GitHub 仓库中找到此模块的其他配置提示和文档。