- 浏览: 1095678 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (290)
- php (65)
- javascript (36)
- html5 (31)
- thinkphp (9)
- mysql (16)
- jquery (13)
- node.js (9)
- css (9)
- android 开发 (8)
- flex (5)
- java (3)
- apache (8)
- linux (8)
- git (5)
- web (5)
- wordpress (9)
- mongodb (2)
- redis (5)
- yaf (6)
- python (4)
- big data (1)
- sphinx (1)
- html (1)
- bootstrap (1)
- vue (1)
- laravel (1)
- test (0)
最新评论
-
July01:
推荐用StratoIO打印控件,支持网页、URL、图片、PD、 ...
如何解决非IE浏览器的web打印 -
flashbehappy:
同一个视频,有mp4,ogg两种格式的。在chrome,fir ...
firefox chrom safari 对video标签的区别 -
xmdxzyf:
可以在网站(www.sosoapi.com)上试下在线表单方式 ...
用swagger-php/ui做API测试 -
flex_莫冲:
a2631500 写道"看了源码,设置Backbon ...
backbone与php交互 -
a2631500:
"看了源码,设置Backbone.emulateJS ...
backbone与php交互
官網地址
https://www.varnish-cache.org
安裝路徑
On Debian/Ubuntu this is /etc/default/varnish
設置Backend servers
/etc/varnish/default.vcl
vcl 4.0;
import directors;
# Default backend definition. Set this to point to your content server.
backend default{
.host = "192.168.2.3";
.port = "80";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
admin console
varnishadm
varnish是緩存在內存的,查看log
varnishlog
varnishlog -g raw
varnishtop -i BereqURL # will show you what your backend is being asked the most.
varnishtop -i ReqURL #will show you what URLs are being asked for by the client
varnishtop -I ReqHeader #Accept-Encoding will show the most popular Accept-Encoding header the client are sending you.
varnishstat #統計
查看版本
varnishd -V
停止
service varnish stop
重启
service varnish restart
特别注意:如果修改了vcl文件,必须重启才会生效
Purging and banning
https://www.varnish-cache.org/docs/4.1/users-guide/purging.html
在default.vcl配置文件中設置允許執行purging操作的ip段,下面表示本機和2.0~2.24的IP都可以執行
# Only allow purging from specific IPs
acl purge {
"localhost";
"127.0.0.1";
"192.168.2.0"/24;
}
# This function is used when a request is send by a HTTP client (Browser)
sub vcl_recv {
call detect_device;
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
# set req.http.Host = regsub(req.http.host, ":[0-9]+", "");
# if (req.http.Host == "*.example.com") {
#set req.backend_hint = test.backend();
# }
# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}
#ban("req.http.host == " + req.http.host + " && req.url == " + req.url);
ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);
# Throw a synthetic page so the
# request won't go to the backend.
return(synth(200, "Ban added"));
}
# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}
# --- Wordpress specific configuration
# Did not cache the RSS feed
if (req.url ~ "/feed") {
return (pass);
}
# Blitz hack
if (req.url ~ "/mu-.*") {
return (pass);
}
# Did not cache the admin and login pages
if (req.url ~ "/wp-(login|admin)") {
return (pass);
}
# Do not cache the WooCommerce pages
### REMOVE IT IF YOU DO NOT USE WOOCOMMERCE ###
#if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") {
# return (pass);
#}
# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^ *$") {
unset req.http.cookie;
}
# Cache the following files extensions
if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
unset req.http.cookie;
}
# Normalize Accept-Encoding header and compression
# https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
# Do no compress compressed files...
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}
# --- End of Wordpress specific configuration
# Did not cache HTTP authentication and HTTP Cookie
#if (req.http.Authorization || req.http.Cookie) {
if (req.http.Authorization) {
# Not cacheable by default
return (pass);
}
# Cache all others requests
return (hash);
}
怎麼判別頁面是varnish生成的?
window在host中添加varnish ip 匹配网站domain,例如
192.168.xx.xx xxx.com
打開website,在console中選擇netword,如果請求項的header中有age說明用了varnish
可以設置多個hos,可以用ip也可以用domain。如果varnish重启了或者purge了缓存,则age会变成0
backend default {
.host = "127.0.0.1";
.port = "8080";
}
We add a new backend.:
backend java {
.host = "127.0.0.1";
.port = "8000";
}
backend server1 {
.host = "server1.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
backend server2 {
.host = "server2.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
sub vcl_init {
new bar = directors.round_robin();
bar.add_backend(server1);
bar.add_backend(server2);
}
--------------
vcl 的正則表達式的寫法
https://gist.github.com/dimsemenov/10100415
http://book.varnish-software.com/3.0/VCL_Basics.html
http://www.hostingadvice.com/how-to/varnish-regex/
-----------------
怎么用PHP删除varnish 指定host或某个url的cache?
为什么要使用X-Purge-Regex?
因为vcl中的req.url获取的是curl的url的path,而这个path是不能传递正则表达式的,因此放到header中,自定义个属性来判断。
使用这个function
https://www.varnish-cache.org
安裝路徑
On Debian/Ubuntu this is /etc/default/varnish
設置Backend servers
/etc/varnish/default.vcl
vcl 4.0;
import directors;
# Default backend definition. Set this to point to your content server.
backend default{
.host = "192.168.2.3";
.port = "80";
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
.max_connections = 800;
}
admin console
varnishadm
varnish是緩存在內存的,查看log
varnishlog
varnishlog -g raw
varnishtop -i BereqURL # will show you what your backend is being asked the most.
varnishtop -i ReqURL #will show you what URLs are being asked for by the client
varnishtop -I ReqHeader #Accept-Encoding will show the most popular Accept-Encoding header the client are sending you.
varnishstat #統計
查看版本
varnishd -V
停止
service varnish stop
重启
service varnish restart
特别注意:如果修改了vcl文件,必须重启才会生效
Purging and banning
https://www.varnish-cache.org/docs/4.1/users-guide/purging.html
在default.vcl配置文件中設置允許執行purging操作的ip段,下面表示本機和2.0~2.24的IP都可以執行
# Only allow purging from specific IPs
acl purge {
"localhost";
"127.0.0.1";
"192.168.2.0"/24;
}
# This function is used when a request is send by a HTTP client (Browser)
sub vcl_recv {
call detect_device;
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
# set req.http.Host = regsub(req.http.host, ":[0-9]+", "");
# if (req.http.Host == "*.example.com") {
#set req.backend_hint = test.backend();
# }
# Allow purging from ACL
if (req.method == "PURGE") {
# If not allowed then a error 405 is returned
if (!client.ip ~ purge) {
return(synth(405, "This IP is not allowed to send PURGE requests."));
}
#ban("req.http.host == " + req.http.host + " && req.url == " + req.url);
ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);
# Throw a synthetic page so the
# request won't go to the backend.
return(synth(200, "Ban added"));
}
# Post requests will not be cached
if (req.http.Authorization || req.method == "POST") {
return (pass);
}
# --- Wordpress specific configuration
# Did not cache the RSS feed
if (req.url ~ "/feed") {
return (pass);
}
# Blitz hack
if (req.url ~ "/mu-.*") {
return (pass);
}
# Did not cache the admin and login pages
if (req.url ~ "/wp-(login|admin)") {
return (pass);
}
# Do not cache the WooCommerce pages
### REMOVE IT IF YOU DO NOT USE WOOCOMMERCE ###
#if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") {
# return (pass);
#}
# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");
# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");
# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");
# Are there cookies left with only spaces or that are empty?
if (req.http.cookie ~ "^ *$") {
unset req.http.cookie;
}
# Cache the following files extensions
if (req.url ~ "\.(css|js|png|gif|jp(e)?g|swf|ico)") {
unset req.http.cookie;
}
# Normalize Accept-Encoding header and compression
# https://www.varnish-cache.org/docs/3.0/tutorial/vary.html
if (req.http.Accept-Encoding) {
# Do no compress compressed files...
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
unset req.http.Accept-Encoding;
}
}
# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
return (pass);
}
if (!req.http.cookie) {
unset req.http.cookie;
}
# --- End of Wordpress specific configuration
# Did not cache HTTP authentication and HTTP Cookie
#if (req.http.Authorization || req.http.Cookie) {
if (req.http.Authorization) {
# Not cacheable by default
return (pass);
}
# Cache all others requests
return (hash);
}
怎麼判別頁面是varnish生成的?
window在host中添加varnish ip 匹配网站domain,例如
192.168.xx.xx xxx.com
打開website,在console中選擇netword,如果請求項的header中有age說明用了varnish
可以設置多個hos,可以用ip也可以用domain。如果varnish重启了或者purge了缓存,则age会变成0
backend default {
.host = "127.0.0.1";
.port = "8080";
}
We add a new backend.:
backend java {
.host = "127.0.0.1";
.port = "8000";
}
backend server1 {
.host = "server1.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
backend server2 {
.host = "server2.example.com";
.probe = {
.url = "/";
.timeout = 1s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
sub vcl_init {
new bar = directors.round_robin();
bar.add_backend(server1);
bar.add_backend(server2);
}
--------------
vcl 的正則表達式的寫法
https://gist.github.com/dimsemenov/10100415
http://book.varnish-software.com/3.0/VCL_Basics.html
http://www.hostingadvice.com/how-to/varnish-regex/
-----------------
怎么用PHP删除varnish 指定host或某个url的cache?
/** * @param url string e.g:/Vip/detail?id=99 * @param path string e.g:/Vip/detail * @param params array,e.g:{id:99} */ public function purgeCache($url = "", $path = "", $params = "") { $varnishHost = C("VARNISH_SERVER"); $regxUrl = ""; if ($url) { $varnishHost. = $url; } elseif($path) { $regxUrl = "^".$path; if (!empty($params)) { $regxUrl. = "(/?)(.*)?"; $regxParams = array(); foreach($params as $key = > $value) { $regxParams[] = "($key=$value|/$key/$value)"; } $regxUrl. = implode("", $regxParams); } $regxUrl. = ".*$"; } //$url = "^/Vip/detail(/?)(\?id=99|/id/99)$"; //^/Finance/detail(/?)(.*)?(id=4178|/id/4178).*$ $method = "PURGE"; $hosts = C("VARNISH_HOSTS"); foreach($hosts as $key = > $frontHost) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_URL, $varnishHost); $headers = array(); $headers[] = 'Connection: Keep-Alive'; $headers[] = "Host: ".$frontHost; if ($regxUrl) { $headers[] = "X-Purge-Regex: $regxUrl"; echo "use regex<br>"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_REFERER, $frontHost); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); //dump($data); $curlErrorCode = curl_errno($ch); if ($curlErrorCode) { //echo "purge varnish cache failed.Curl error: " . curl_error($ch); Log::write("purge varnish cache failed.Curl error: ".curl_error($ch)); } curl_close($ch); } return true; }
为什么要使用X-Purge-Regex?
因为vcl中的req.url获取的是curl的url的path,而这个path是不能传递正则表达式的,因此放到header中,自定义个属性来判断。
使用这个function
public function purgeTest() { $url = ""; $path = "/Article/detail"; $params = array("id"=>4178); $this->purgeCache($url, $path, $params); }
发表评论
-
将博客搬至CSDN
2017-03-28 09:07 694将博客搬至CSDN,尽情期待 -
laravel入門
2017-03-03 16:31 6851 全局安裝 composer global require ... -
导出csv,excel等文件,文件内容错误的问题,需要加上ob_end_clean
2017-03-02 11:54 1597见这个问题: http://stackoverflow.com ... -
facebook graph api从2.6转到2.8的问题处理
2017-02-15 16:30 1240之前一直用facebook 2.6的api,现在想升级到2.8 ... -
php从mysql读取超过200W行的表
2017-01-04 10:09 688需要从一个大表,如user表中读取所有user数据再做整理并導 ... -
一个session失效的伪命题
2016-12-08 18:33 779最近遇到一个问题,一个ajax请求会调用已存在的session ... -
tp5与tp3的区别
2016-11-21 14:33 1572TP5作了很大的改变,更加灵活,没那么臃肿了,加入了一些令人振 ... -
推送消息能不能区分禁止通知和卸载两种类型?
2016-11-18 10:54 1514消息推送ios用了apns,android用的是gcm。推送失 ... -
yaf在命令行模式下出现PHP Fatal error: Class 'Yaf_Application' not found的解决办法
2016-11-11 16:42 2428为了跑个yaf的crontab,执行时报错: PHP Fata ... -
推送emoji表情乱码的解决方法
2016-11-04 17:44 1996推送内容如果包含了emoji表情,需要做以下修改 1 mysq ... -
YAF访问图片等静态资源禁止调用YAF框架的解决办法
2016-10-20 17:55 1657当访问yafpublic 目录下的某个静态资源时,依然会访问y ... -
在线自动协同编辑文本的实现方案
2016-10-14 16:49 2002要实现可以自动在线协同编辑文本的功能。就类似google do ... -
Paypal支付跳转失败的原因及解决办法
2016-09-18 14:27 3654遇到在跳转到paypal支付页面的时候出现502 bad ga ... -
文件格式不是unix导致sed,cat等读取文件后无法正常退出
2016-08-30 14:44 814#!/bin/bash bucket_config=&quo ... -
TP在APP_DEBUG=false的情況下,CLI和WEBSITE會共用同一個~runtime.php導致出錯的問題解決辦法
2015-12-14 11:43 867TP若使用了 define('APP_DEBU ... -
node.js获取php curl post数据req.body为空的处理
2015-09-22 11:43 3991node使用了express4和body-parser来解析p ... -
yaf namespace的使用
2015-08-27 11:32 2344yaf支持namespace。在php.ini 中添加 yaf ... -
yaf 添加phpunittest
2015-08-21 11:16 661首先要讓php在cli下支持yaf wamp下打開D:/wam ... -
thinkphp socket to support sso (php socket 不稳定,不建议使用)
2015-08-18 14:06 2037cli code #!/bin/bash filep ... -
TP的module支持中文
2015-05-20 14:06 1317為了讓URL支持中文,例如www.xxx.com/廣州/xx ...
相关推荐
除了VCL中的配置,你还可以通过连接到Varnish的管理端口(默认为6000或3500)并执行特定的CLI命令来清除缓存。例如,对于Varnish 2.1,你可以使用以下命令: ``` telnet 192.168.1.185 3500 purge.url regex_...
一款高性能的开源HTTP加速器,2006年发布的第一个版本0.9,发展到...挪威最大的在线报纸 Verdens Gang 使用3台Varnish代替了原来的12台Squid,性能比以前更好。 2.作者:Poul-Henning Kamp是FreeBSD的内核开发者之一。
nginx和php和varnish配置
8. Varnish的缓存更新和失效策略:了解如何设置缓存过期时间、如何根据内容变化自动更新缓存,以及如何使用Ban命令手动清除特定缓存项,都是使用Varnish时的关键技能。 9. Varnish在API和微服务架构中的应用:...
Varnish是一款高性能、开源的反向代理服务器和缓存服务器,其开发者Poul-Henning Kamp是FreeBSD核心的开发人员之一。Varnish采用全新的软件体系结构,和现在的硬件体系配合比较紧密。
本书是介绍如何快速使用Varnish的一本电子书,英文的
你可以使用`varnishstat`命令查看Varnish的统计信息,以便了解缓存效果和调整策略。同时,监控Lighttpd的日志,确保没有错误发生。 **四、Varnish+Lighttpd的优化** 1. **缓存策略优化**:根据网站内容特点,调整...
第一个函数clearVarnish是一个更通用的函数,它接受三个参数:$ip为Varnish服务器的IP地址,$url是要清除缓存的URL,$host为可选参数,用于指定需要清空缓存的主机名。函数内部通过遍历一个数组,尝试对每一个...
Varnish的主要目标是提高网站的响应速度和整体性能,通过缓存网页内容来减少对服务器的直接请求。自2006年发布首个0.9版本以来,Varnish因其高效能、低资源占用和稳定性,已经在许多大型网站上取代了如Squid这样的...
下面将详细介绍Varnish的原理、功能以及如何安装和使用3.0.7版本。 **Varnish基础概念** Varnish是一个开源的反向代理服务器,它的主要作用是接收HTTP请求,并根据预设的策略缓存网页内容。当后续的请求相同内容时...
安装并配置好Varnish后,可以使用以下命令启动和停止服务: ```bash sudo systemctl start varnish sudo systemctl enable varnish sudo systemctl status varnish sudo systemctl stop varnish ``` 四、监控与...
《Varnish权威指南》是一本深度探讨Varnish缓存服务器技术的专业书籍,中文版的出版使得更多国内用户能够无障碍地学习和掌握这一高效的内容分发和加速工具。Varnish作为一个开源的HTTP加速器,它在Web性能优化领域...
2. 更灵活的缓存策略:提供了更多自定义缓存规则的选项,用户可以通过VCL(Varnish Configuration Language)编写自己的缓存策略,实现对不同内容类型和URL的精细化控制。 3. 改进的API和管理工具:提供更强大的...
4. **不允许非访问控制列表的IP进行varnish缓存清除**: 如配置文件中的`if (!client.ip ~ local)`判断。 5. **清除URL中有特定文件类型的Cookie**: 例如清除`.jpg`、`.png`等图片格式文件的Cookie。 6. **取消服务器...
Varnish的配置和使用对于提升网站性能至关重要。 ### 测试报告的目的 这份测试报告的目的是评估Varnish缓存在不同并发量下的性能表现。通过模拟多用户并发访问,观察Varnish缓存对Web服务的响应时间、吞吐量等关键...
5. **与其他缓存的集成**:Varnish可以与Magento的其他缓存机制如Full Page Cache (FPC) 结合使用,以实现最佳性能。 6. **更新和维护**:随着Magento和Varnish版本的更新,需要定期检查并更新配置,以确保兼容性和...
这得益于Varnish的高效内存管理,它使用一种称为“存储桶”的数据结构来高效地存储和检索缓存内容,同时Varnish的事件驱动、异步非阻塞I/O模型使其能处理大量并发连接。 Varnish 4.0.3版本可能包含以下组件: 1. **...