使用不带$ args的原始$ request_uri设置fastcgi_cache_key [英] Set fastcgi_cache_key using original $request_uri without $args
问题描述
我正在尝试集成WordPress插件(Jetpack的Related Posts模块),该插件将查询字符串添加到帖子URL的末尾.我想用FastCGI缓存URL,而完全忽略查询字符串/$args
.
I am trying to integrate a WordPress plugin (Jetpack's Related Posts module) which adds query strings to the end of post URLs. I would like to cache URLs with FastCGI while completely ignoring the query strings/$args
.
我当前的配置是:fastcgi_cache_key "$scheme$request_method$host$request_uri";
我知道使用此处提到的解决方案,对于包含特定$arg
的URL,我的$skip_cache
变量将关闭,这是可行的.但是,无论$args
的值如何,我都希望缓存相同的结果,而不是为每组$args
使用唯一的缓存键.
I am aware of using the solution mentioned here to turn my $skip_cache
variable off for URLs containing a certain $arg
, which works. However, I want to cache the same result regardless of the value of the $args
rather than using a unique cache key for each set of $args
.
我也知道一些建议,只需在fastcgi_cache_key
而不是$request_uri
;但是,由于$uri
不仅是原始请求的URI减去$args
,因此WordPress体系结构中的某些内容(可能是漂亮的链接)会强制所有请求的URI返回相同的缓存结果(而不是每个页面都返回不同的结果).
I am also aware of some suggestions to just use $uri
in the fastcgi_cache_key
rather than $request_uri
; however, because $uri
is not just the original requested URI minus the $args
, something in the WordPress architecture (likely pretty links) forces all requested URIs to return the same cache result (rather than a different result for each page).
有什么方法可以真正使用最初请求的URI,而不必在缓存键中包含$args
?
Is there any way to truly use the originally requested URI without including the $args
in the cache key?
推荐答案
直到现在,我也遇到了类似的问题. 所以,我的解决方案:
Just now, I had similar problem. So, my solution:
在nginx配置中添加到
In the nginx config add to the
http {
...
map $request_uri $request_path {
~(?<captured_path>[^?]*) $captured_path;
}
...
}
然后,您将有一个变量$ request_path,其中包含$ request_uri,而没有query_string.
Then you will have a variable $request_path, which contains $request_uri without query_string.
因此使用$ request_path作为缓存键
So use $request_path for cache key
fastcgi_cache_key "$scheme$request_method$host$request_path"
重要. "map"伪指令只能添加到"http {}"中.该指令将对所有主机的所有请求执行.
Important. The "map" directive can be added only to "http {}". This directive will be execute for all requests for all hosts.
这篇关于使用不带$ args的原始$ request_uri设置fastcgi_cache_key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!