![Nginx 配置处理来自 Redis 的请求](https://linux22.com/image/621844/Nginx%20%E9%85%8D%E7%BD%AE%E5%A4%84%E7%90%86%E6%9D%A5%E8%87%AA%20Redis%20%E7%9A%84%E8%AF%B7%E6%B1%82.png)
我正在尝试使用 redis 缓存配置直接从 nginx 处理请求,缓存已经添加到 redis 问题是配置 nginx 从 redis 获取缓存
URL 就像http://example.com/action.js?param1=10¶m2=text
Redis 的 key 依赖于 param1
在以下示例中,它使用完整路径作为 redis 的键
所以我想捕获 param1 来获取类似“cahced_page_”+param1 的密钥
server {
listen 80;
server_name your.website.com;
root /home/appuser/app/current/public;
error_log /dev/null crit; #real man don't log
location / {
set $redis_db "1";
set $redis_key $uri;
default_type text/html;
redis_pass redis;
error_page 404 405 502 504 = @fallback;
}
location @fallback {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://yourunicornupstream;
break;
}
}
}
答案1
从http://wiki.nginx.org/HttpEchoModule#Variables
location = /code.js {
set $redis_db "1";
set $redis_key "hashed_key_$arg_param1";
default_type text/javascript;
redis_pass redis;
error_page 404 405 502 504 = @fallback;
}