You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求适用于Nginx开源版的Nginx Plus键值存储模块替代方案

Nginx开源版替代Plus键值存储模块的方案

以下是几种成熟的替代方案,覆盖不同场景需求:

  • 基于外部键值存储(Redis/Memcached)+ Nginx官方模块
    Nginx开源版自带ngx_http_memcached_module,也可以编译第三方的ngx_http_redis2_module,直接和外部Redis/Memcached服务交互,实现键值的读写操作,适合需要持久化或分布式共享的场景。
    Redis配置示例:

    location /kv/set {
        redis2_query SET $arg_key $arg_val;
        redis2_pass your-redis-host:6379;
    }
    
    location /kv/get {
        redis2_query GET $arg_key;
        redis2_pass your-redis-host:6379;
    }
    

    Memcached配置示例:

    location /kv/get {
        set $memcached_key $arg_key;
        memcached_pass your-memcached-host:11211;
        error_page 404 = /kv/not-found;
    }
    
    location /kv/not-found {
        return 404 "Key does not exist";
    }
    
  • 静态键值映射:ngx_http_map_module
    如果你的键值对是静态、不需要动态修改的,map模块完全可以替代。它支持在配置中定义键值映射关系,通过请求变量匹配并返回对应值,修改配置后需重启Nginx生效。
    配置示例:

    map $http_user_agent $backend_server {
        ~*chrome     backend-chrome:8080;
        ~*firefox    backend-firefox:8080;
        default      backend-default:8080;
    }
    
    server {
        listen 80;
        location / {
            proxy_pass http://$backend_server;
        }
    }
    
  • 内存级动态键值存储:OpenResty Lua模块
    借助OpenResty的ngx_http_lua_module(可单独编译到Nginx开源版),使用lua_shared_dict实现内存中的键值存储,支持动态读写,性能优异,但数据会随Nginx重启丢失,适合临时缓存或动态配置场景。
    配置示例:

    http {
        # 定义10MB的共享内存字典
        lua_shared_dict kv_store 10m;
    
        location /kv/set {
            content_by_lua_block {
                local key = ngx.var.arg_key
                local val = ngx.var.arg_val
                if key and val then
                    ngx.shared.kv_store:set(key, val)
                    ngx.say("Key set successfully")
                else
                    ngx.say("Missing key or value parameter")
                end
            }
        }
    
        location /kv/get {
            content_by_lua_block {
                local key = ngx.var.arg_key
                local val = ngx.shared.kv_store:get(key)
                ngx.say(val or "Key not found")
            }
        }
    }
    

内容的提问来源于stack exchange,提问作者Anna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 05:40:25