我有一个托管在 Firebase 上的网站,使用静态 html,没有使用服务器端功能来传递结果。
运行时
curl -X PURGE https://mywebsite.com -v -L
结果是:
{ "status": "ok", "id": "20755-1619059392-3560756" }
我需要一种方法来将此操作限制为特定 IP,这样任何人都无法重置我的缓存,这可能会导致额外的费用。
此外,Firebase 似乎使用 Varnish 来管理缓存(这是空的)。
我客户的安全顾问向我们发送了有关如何处理此问题的建议,我不确定这到底是 .htaccess 语法还是什么:
# Varnish recommends to using PURGE method only by valid user,
# for example by ip limiting and for other return 405 Not allowed:
acl purge {
"localhost";
"192.168.55.0"/24;
}
sub vcl_recv {
# allow PURGE from localhost and 192.168.55...
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405,"Not allowed."));
}
return (purge);
}
}
我不知道如何在 Firebase Hosting 中应用此功能,同样我不使用服务器功能,只是使用常规的
firebase.json
和以下标题:
"headers": [
{
"source": "*.[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].+(css|js)",
"headers": [
{
"key": "Cache-Control",
"value": "public,max-age=31536000,immutable"
}
]
},
{
"source": "**/*.@(json|eot|otf|ttf|ttc|woff|font.css)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
}
]
以下代码是VCL代码:
acl purge {
"localhost";
"192.168.55.0"/24;
}
sub vcl_recv {
# allow PURGE from localhost and 192.168.55...
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405,"Not allowed."));
}
return (purge);
}
}
此代码允许您扩展 Varnish 的行为。必须将此代码添加到您的
/etc/varnish/default.vcl
文件中。
将此代码添加到您的 VCL 文件后,您必须重新加载您的
varnishd
进程才能激活此 VCL 配置。
如果无法重新加载
varnishd
,您还可以使用以下命令激活新的VCL文件:
sudo varnishadm vcl.load purge_acl /etc/varnish/default.vcl
sudo varnishadm vcl.use purge_acl
有关 Varnish 和 VCL 编程语言的更多信息,请查看 http://varnish-cache.org/docs/6.0/reference/index.html
暂时没有解决办法。这是我从 firebase 支持处收到的答案:
嗨达米安,
我叫谢尔盖,感谢您与我联系。我会帮助你的。
这里首先要解决的是 Varnish 服务 超出了我们的范围,因此我们有关其的信息 我们的托管服务的实现并不是最丰富的。
不幸的是,现在我们只能用以下方式控制缓存行为 我们现有的工具。
很抱歉我们当时无法提供您需要的功能, 如果您愿意,我们可以提交功能请求,所以这是 我们的工程团队可见。
如果 Firebase Hosting 没有解决方案或者 Varnish 不可用, 也许尝试不同的解决方案,例如使用云函数。
firebase.json
...
"rewrites": [
{
"source": "**/*.@(json|eot|otf|ttf|ttc|woff|font.css)",
"function": "no_purge"
}
]
...
/functions/index.js(云函数)
const functions = require('firebase-functions');
exports.no_purge = functions.https.onRequest((req, res) => {
if(req.method == 'PURGE') { /* complete this condition to allow specific IPs */
res.status(405).send('<!doctype html>
<head>
<title>PURGE Not allowed</title>
</head>
<body>
PURGE Not allowed
</body>
</html>');
}
});
我知道如果您想要一个简单的动态解决方案,Varnish更合适。