# lua-nginx-cache **Repository Path**: zhhnch/lua-nginx-cache ## Basic Information - **Project Name**: lua-nginx-cache - **Description**: OpenResty Lua nginx cache - **Primary Language**: Lua - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-01-22 - **Last Updated**: 2021-02-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Lua增强nginx缓存 ## 介绍 支持poxy_cache、fastcgi_cache 支持post参数json、x-www-form-urlencoded ## 使用方法 ``` proxy_cache_path /usr/local/var/tmp/nginx levels=1:2 keys_zone=test_cache:10m inactive=10m max_size=50m; server { listen 8888; default_type text/plain; location / { set $cache_key ''; set $cache_ignore_params 'param_key1,param_key2,param_key3'; rewrite_by_lua_file "lua-nginx-demo/src/lua/cache-key.lua"; proxy_cache_key $cache_key; proxy_cache_methods GET POST; proxy_cache test_cache; proxy_cache_lock on; proxy_cache_min_uses 1; proxy_cache_valid 200 20s; proxy_cache_lock_timeout 5s; proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 ; proxy_pass http://127.0.0.1:7788; } } ``` src/lua/cache-key.lua脚本负责根据 request_method(请求方法), uri,uri_args(uri参数), post_args(post参数)生成cache_key cache_key示例: ``` POST /cache/test?param_key1=value1¶m_key2=value2¶m_key3=value3 ``` post_args会覆盖uri_args的同名参数 使用$cache_ignore_params 变量设置缓存无效的参数,多个参数之间使用逗号","隔开 当$cache_ignore_params为空时则会根据所有参数生成cache_key ``` set $cache_key ''; set $cache_ignore_params 'param_key1,param_key2,param_key3'; rewrite_by_lua_file "lua-nginx-demo/src/cache-key.lua"; proxy_cache_key $cache_key; ```