php 如何在清漆中发送清除请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9215617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a purge request in varnish
提问by flukeflume
I can't see a similar question, but apologies if I'm duping.
我看不到类似的问题,但如果我被欺骗了,我深表歉意。
We're running a varnish cache on our system, but want to install a system where we can purge individual pages when they are edited (fairly normal). We've been trying to get it to work by using an HTTP header. So, our VCL is set up like:
我们在我们的系统上运行一个清漆缓存,但想要安装一个系统,我们可以在编辑时清除单个页面(相当正常)。我们一直在尝试通过使用 HTTP 标头使其工作。因此,我们的 VCL 设置如下:
acl purge {
"localhost";
#### Our server IP #####
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
}
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
}
}
However, I'm stuck on how to actually SEND the http purge request. We're using PHP for the website, so I've tried using:
但是,我被困在如何实际发送 http 清除请求上。我们在网站上使用 PHP,所以我尝试使用:
header("PL: PURGE / HTTP/1.0");
header("Host: url to purge");
But this doesn't seem to do anything (and varnishlog doesn't seem to show anything purging).
但这似乎没有任何作用(并且 varnishlog 似乎没有显示任何清除)。
I've also experimented with cURL but, again, it doesn't seem to be working. Am I missing something really basic here, or is the basis sound, meaning my implementation is bugged?
我也尝试过 cURL,但同样,它似乎不起作用。我是否在这里遗漏了一些非常基本的东西,或者是基本的声音,这意味着我的实现有问题?
Many thanks,
非常感谢,
回答by Jeremy Roman
You need to go and make an HTTP request.
你需要去发出一个 HTTP 请求。
Untested, but should be along the lines of (if you want to use curl as you mentioned):
未经测试,但应该遵循(如果你想使用你提到的 curl ):
$curl = curl_init("http://your.varnish.cache/url-to-purge");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE");
curl_exec($curl);
回答by Dario Fumagalli
Quick, dirty and effective way to send a PURGE request:
发送 PURGE 请求的快速、肮脏和有效的方式:
curl -v -k -X PURGE URL
You can actually make a little script with that statement, as follows:
您实际上可以使用该语句制作一个小脚本,如下所示:
1) Open an editor, in example VI
1)打开一个编辑器,在例子VI中
vi varnish_purge_url.sh
2) Enter the following text:
2) 输入以下文字:
#!/bin/sh
curl -v -k -X PURGE
(remember to leave the blank line between the first and last lines).
(记得在第一行和最后一行之间留空行)。
3) Save the file and exit. Then set the appropriate attributes to execute it from the shell:
3) 保存文件并退出。然后设置适当的属性以从 shell 执行它:
chmod 750 varnish_purge_url.sh
4) You want to be root when creating and using the above script. If using Ubuntu, feel free to add sudo
in front of commands when required.
4) 在创建和使用上述脚本时,您想成为 root。如果使用 Ubuntu,请sudo
在需要时随意在命令前添加。
5) Usage is simple:
5) 使用简单:
./varnish_purge_url.sh URL
Where URL
is the URL to purge.
URL
要清除的 URL在哪里。
回答by nish
You can also purge using command line. Use the command sudo varnishadm
. This will open the Varnish Command-line Interface. where you can type in your command to purge the pages as per your need. E.g to purge your home page, do this:
您还可以使用命令行清除。使用命令sudo varnishadm
。这将打开 Varnish 命令行界面。您可以在其中输入命令以根据需要清除页面。例如,要清除您的主页,请执行以下操作:
root@staging:/etc/varnish# sudo varnishadm
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.5.0-28-generic,x86_64,-sfile,-smalloc,-hcritbit
Type 'help' for command list.
Type 'quit' to close CLI session.
varnish> ban.url ^/$
200
回答by JaseC
Maybe I'm on a newer version but what's above didn't work for me. This did:
也许我使用的是较新版本,但上面的内容对我不起作用。这做到了:
sub vcl_hit {
if (req.request == "PURGE") {
ban("req.url ~ "+req.url);
error 200 "Purged.";
}
}