[文章作者:张宴 本文版本:v1.2 最后修改:2008.01.02 转载请注明出处:http://blog.zyan.cc]

  我曾经写过一篇文章──《初步试用Squid的替代产品──Varnish Cache网站加速器》,但当时仅仅是用着玩,没做深入研究。

  今天写的这篇关于Varnish的文章,已经是一篇可以完全替代Squid做网站缓存加速器的详细解决方案了。网上关于Varnish的资料很少,中文资料更是微乎其微,希望本文能够吸引更多的人研究、使用Varnish。

  在我看来,使用Varnish代替Squid的理由有三点:
  1、Varnish采用了“Visual Page Cache”技术,在内存的利用上,Varnish比Squid具有优势,它避免了Squid频繁在内存、磁盘中交换文件,性能要比Squid高。
  2、Varnish的稳定性还不错,我管理的一台图片服务器运行Varnish已经有一个月,没有发生过故障,而进行相同工作的Squid服务器就倒过几次。
  3、通过Varnish管理端口,可以使用正则表达式快速、批量地清除部分缓存,这一点是Squid不能具备的。

  点击在新窗口中浏览此图片


  下面来安装Varnish网站缓存加速器(Linux系统):
  1、创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache


  2、创建Varnish日志目录(/var/logs/):
mkdir -p /var/logs
chmod +w /var/logs
chown -R www:www /var/logs


  3、编译安装varnish:
wget http://blog.zyan.cc/soft/linux/varnish/varnish-1.1.2.tar.gz
tar zxvf varnish-1.1.2.tar.gz
cd varnish-1.1.2
./configure --prefix=/usr/local/varnish
make && make install


  4、创建Varnish配置文件:
vi /usr/local/varnish/vcl.conf

  输入以下内容:
引用
backend myblogserver {
       set backend.host = "192.168.0.5";
       set backend.port = "80";
}

acl purge {
       "localhost";
       "127.0.0.1";
       "192.168.1.0"/24;
}

sub vcl_recv {
       if (req.request == "PURGE") {
               if (!client.ip ~ purge) {
                       error 405 "Not allowed.";
               }
               lookup;
       }

       if (req.http.host ~ "^blog.zyan.cc") {
               set req.backend = myblogserver;
               if (req.request != "GET" && req.request != "HEAD") {
                       pipe;
               }
               else {
                       lookup;
               }
       }
       else {
               error 404 "Zhang Yan Cache Server";
               lookup;
       }
}

sub vcl_hit {
       if (req.request == "PURGE") {
               set obj.ttl = 0s;
               error 200 "Purged.";
       }
}

sub vcl_miss {
       if (req.request == "PURGE") {
               error 404 "Not in cache.";
       }
}

sub vcl_fetch {
       if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
               set obj.ttl = 3600s;
       }
       else {
               set obj.ttl = 30d;
       }
}

  这里,我对这段配置文件解释一下:
  (1)、Varnish通过反向代理请求后端IP为192.168.0.5,端口为80的web服务器;
  (2)、Varnish允许localhost、127.0.0.1、192.168.0.***三个来源IP通过PURGE方法清除缓存;
  (3)、Varnish对域名为blog.zyan.cc的请求进行处理,非blog.zyan.cc域名的请求则返回“Zhang Yan Cache Server”;
  (4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;
  (5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

  5、启动Varnish
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on


  6、启动varnishncsa用来将Varnish访问日志写入日志文件:
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/varnish.log &


  7、配置开机自动启动Varnish
vi /etc/rc.local

  在末尾增加以下内容:
引用
ulimit -SHn 51200
/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file,/var/vcache/varnish_cache.data,1G -g www -u www -w 30000,51200,10 -T 127.0.0.1:3500 -p client_http11=on
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &


  8、优化Linux内核参数
vi /etc/sysctl.conf

  在末尾增加以下内容:
引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000    65000



  再看看如何管理Varnish:
  1、查看Varnish服务器连接数与命中率:
/usr/local/varnish/bin/varnishstat

  点击在新窗口中浏览此图片

  2、通过Varnish管理端口进行管理:
  用help看看可以使用哪些Varnish命令:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 help

引用
Available commands:
ping [timestamp]
status
start
stop
stats
vcl.load
vcl.inline
vcl.use
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool


  3、通过Varnish管理端口,使用正则表达式批量清除缓存:
  (1)、例:清除类似http://blog.zyan.cc/a/zhangyan.html的URL地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/

  (2)、例:清除类似http://blog.zyan.cc/tech的URL地址:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge w*$

  (3)、例:清除所有缓存:
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge *$


  4、一个清除Squid缓存的PHP函数(清除Varnish缓存同样可以使用该函数,无需作任何修改,十分方便):


  附1:Varnish官方网站:http://www.varnish-cache.org/

  附2:2007年12月10日,我写了一个每天0点运行,按天切割Varnish日志,生成一个压缩文件,同时删除上个月旧日志的脚本(/var/logs/cutlog.sh):
  /var/logs/cutlog.sh文件内容如下:
引用
#!/bin/sh
# This file run at 00:00
date=$(date -d "yesterday" +"%Y-%m-%d")
pkill -9 varnishncsa
mv /var/logs/youvideo.log /var/logs/${date}.log
/usr/local/varnish/bin/varnishncsa -n /var/vcache -w /var/logs/youvideo.log &
mkdir -p /var/logs/youvideo/
gzip -c /var/logs/${date}.log > /var/logs/youvideo/${date}.log.gz
rm -f /var/logs/${date}.log
rm -f /var/logs/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz

  设置在每天00:00定时执行:
  
/usr/bin/crontab -e
  或者  
vi /var/spool/cron/root
  输入以下内容:
引用
0 0 * * * /bin/sh /var/logs/cutlog.sh



Tags: , , ,



技术大类 » Cache与存储 | 评论(8651) | 引用(0) | 阅读(805495)
Cuevana Email Homepage
2024-8-21 19:21
In the ever-expanding universe of online streaming platforms, cuevana stands out as a pioneer, a disruptor, and a testament to the power of innovation in the digital age.
Emaar Palm Heights Email Homepage
2024-8-22 16:38
Discover the epitome of modern living at Emaar Palm Heights, located in the prime area of Sector 77, Gurgaon. Offering spacious 3BHK + Lounge apartments, this premium residential project by Emaar is designed to elevate your lifestyle. Whether you're seeking tranquility or connectivity, Emaar Palm Heights Gurgaon provides the perfect blend, with easy access to major highways, top-notch amenities, and lush green surroundings. Embrace a life of comfort and elegance at Emaar Palm Heights Sector 77 Gurgaon, where every detail is crafted to perfection.Visit our website - https://emaarpropty.com/emaar-palm-heights/
johanrose Email Homepage
2024-8-22 17:53
Ledger Live is a comprehensive platform designed to manage your cryptocurrency assets securely. ledger live | Available as both a desktop and mobile application, it allows users to buy, sell, exchange, and stake a wide range of cryptocurrencies
Emaar Emerald Hills Email Homepage
2024-8-22 19:11
Discover the epitome of luxury living at Emaar Emerald Hills, a prestigious residential community nestled in the heart of Gurgaon. Located in one of the most sought-after areas, Emaar Emerald Hills Gurgaon offers an exquisite blend of modern amenities and lush green surroundings. Whether you are looking to build your dream home or invest in a prime piece of real estate, Emaar Emerald Hills Phase 2 provides an unparalleled opportunity to experience a life of comfort and sophistication. Enjoy the convenience of being close to major business hubs, shopping centers, and educational institutions, all while residing in a serene and secure environment.Visit our website - https://emaarpropty.com/emaar-emerald-hills-phase-2/
Emaar Marbella Email Homepage
2024-8-22 20:14
Experience the epitome of luxury living at Emaar Marbella, an exclusive residential project nestled in the heart of Gurgaon. Located in Sector 66, Emaar Marbella Gurgaon offers a blend of contemporary design and Spanish architecture, making it a prestigious address for those who appreciate elegance and comfort. Marbella Gurgaon is not just a residence but a lifestyle, providing spacious villas with world-class amenities that cater to every need. Whether you're looking for a serene environment or a vibrant community, Emaar Marbella Sector 66 is the perfect place to call home.Visit our website - https://emaarpropty.com/emaar-marbella-phase-2/
Cannabis Email Homepage
2024-8-23 04:09
I really appreciate the extra work you took on to make sure that your blog will be quite impressive.Cannabis Dispensary
Krisumi Waterfall Residence Email Homepage
2024-8-23 17:39
Krisumi Waterfall Residence  is an exquisite residential development in Sector 36A, Gurugram, that brings together the best of Japanese craftsmanship and Indian luxury. Developed by Krisumi Corporation, a collaboration between Sumitomo Corporation of Japan and Krishna Group of India, this landmark project offers elegantly designed homes with spacious layouts, high-end finishes, and modern amenities.
Walmart Gift Card Balance Email Homepage
2024-8-24 17:17
You can check the balance of your Walmart credit card balance using three different ways. Here are a few ways to verify your Walmart credit card balance. You can determine the amount of money in you Walmart gift card accessible right at the moment.Walmart Gift Card Balancewalmart Gift Card Balancevanilla Gift Card Balancemacy's Gift Card Balance
miss envy cb Email Homepage
2024-8-25 04:58
I wanted to give you a credit for providing the awesome and winning ideas in this blog. It’s very impressive.miss envy cbd
cali bubba kush Email Homepage
2024-8-25 11:31
https://herbapproach.org/product-category/cannabis-flower/cali-bubba-kush/Thanks for the useful article
dilaralaurier Email Homepage
2024-8-26 20:15
The Home Depot(r) assists people to make more use of the time they have and with their money. From free shipping on over one million items online on homedepot.com to voice and image search within Our award-winning application. Home Depot Home Depot makes shopping for home improvement more convenient than ever. home depot gift card balance |home depot gift card balance | Apple gift card balance | Nordstrom gift card balance | chilis gift card balance | macy's gift card balance | macy's gift card balance
Gurgaon properties Email Homepage
2024-8-27 18:24
Premier residential project Godrej Vriksha, which offers opulent living quarters in a bustling neighborhood, is now under construction in Sector 103, Gurgaon. Modern style and top-notch amenities are combined in <a href="https://godrejpropertis.in/godrej-vriksha-sector-103/" target="_blank">Godrej Vriksha Sector 103</a>, the newest addition to the esteemed portfolio of Godrej. Godrej Projects' latest offering, which is ideally situated in Gurgaon and surrounded by lush vegetation, guarantees easy access to important hubs. Godrej Vriksha Sector 103 Gurgaon offers the ideal fusion of peaceful living in an urban environment. In one of Gurgaon's most sought-after locations, don't pass up the chance to join this elite neighborhood.<a href="https://godrejpropertis.in/godrej-air-phase-ii/" target="_blank">Godrej Air Phase II</a>, located in the desirable Sector 85, Gurgaon, offers the height of luxury and relaxation. This magnificent residential development by Godrej Properties is intended to improve your lifestyle by providing the ideal fusion of contemporary conveniences and tranquil living.You and your family may live a healthy and serene life at Godrej Air Gurgaon since every element has been thoughtfully designed to provide a refreshing environment. The project showcases well designed apartments with roomy floor plans that provide an insight into a luxurious lifestyle.Explore Godrej Habitat, an opulent residential development located in Sector 3 in Gurgaon. With the carefully considered Godrej Habitat Floor Plans, which provide the ideal balance of comfort and space, you may embrace a calm lifestyle. Discover the <a href="https://godrejpropertis.in/godrej-habitat/" target="_blank">Godrej Habitat</a> Sector 3 location, which offers great connectivity and easy access to important sites. This project is your doorway to opulent life in Gurgaon, regardless of your need for specifics regarding the Godrej Habitat Price or information about the amenities provided. Godrej Habitat offers the height of elegance and convenience.
jackma Email
2024-8-27 19:22
Ledger Live app is a safe and easy interface for managing your cryptocurrencies using your Ledger device. Unlike most apps, the Ledger Live crypto wallet. Ledger Live plays a crucial role in enhancing the security and accessibility of cryptocurrency management. By providing a user-friendly interface and seamless integration with Ledger hardware wallets.
Gurgaon Properties Email Homepage
2024-8-28 17:38
Godrej Vriksha is an exquisite residential property located in Sector 103, Gurgaon that offers the ultimate in luxury living. The perfect fusion of contemporary conveniences and tranquil natural surroundings can be found in this Godrej New Launch. Godrej Vriksha Sector 103, which is ideally situated in one of Gurgaon's most desirable neighbourhoods, offers connection and convenience unlike anything else. Discover luxurious living areas that have been carefully planned to meet your needs. Don't pass up this wonderful chance to join Godrej Projects in Sector 103 in Gurgaon. Put money into your future now with Godrej Vriksha!Visit - https://godrejpropertis.in/godrej-vriksha-sector-103/Discover the luxurious Godrej Air Phase II residential project in Sector 85, Gurgaon, which provides an unmatched quality of life. Godrej Air Sector 85 is tucked away in one of the most desirable areas in Gurgaon and is built to offer a calm and healthy lifestyle. The project's carefully designed floor designs meet the various needs of contemporary families. Are you wondering how much it will cost? When taking into account the opulent amenities and desirable location, the Godrej Air Phase II Price is competitive and provides good value for money. The Godrej Air Floor Plan's clever design and effective use of space will satisfy your needs whether you're searching for a large apartment or a well-organized living area.Visit - https://godrejpropertis.in/godrej-air-phase-ii/Visit Godrej Habitat in Sector 3, Gurgaon, to experience luxurious living. Enjoy an unparalleled lifestyle with well-thought-out floor plans that meet all of your requirements. Godrej Habitat offers comfortable homes with a combination of contemporary conveniences and tranquil surroundings. Look through the many Godrej Habitat floor plans to see which one is best for your family. Godrej Habitat provides outstanding value for your investment in Gurgaon's ideal location at cheap cost. Check out the most recent Godrej Habitat rates now to ensure you don't miss the opportunity to own a piece of this exclusive community!Visit - https://godrejpropertis.in/godrej-habitat/
Gurgaon Properties Email Homepage
2024-8-28 19:04
Discover Elan Mercado, an exclusive mixed-use development located in Sector 80, Gurgaon. This iconic project blends luxury retail spaces with premium residential units, offering a vibrant lifestyle in the heart of Gurgaon. Elan Mercado Gurgaon is designed to cater to the needs of modern urbanites, with state-of-the-art amenities, high-end shopping outlets, and gourmet dining options. Positioned strategically in Sector 80, Gurgaon, Elan Mercado provides unparalleled connectivity to major business hubs, making it an ideal investment destination.Visit- https://elaninfra.in/elan-mercado/Experience unparalleled luxury and modern amenities at Elan The Mark in Gurgaon. Located in the prime Sector 106, Elan The Mark offers a perfect blend of elegance and convenience. Whether you're looking for a residential space or a commercial property, Elan The Mark 106 sets the standard for upscale living and business opportunities in Gurgaon. Explore the future of sophisticated urban living at Elan The Mark Gurgaon today!Visit- https://elaninfra.in/elan-the-mark/
mahira Email
2024-8-29 19:10
OneKey Wallet is a secure, user-friendly cryptocurrency wallet that supports multiple blockchain assets. It offers seamless integration with hardware wallets, advanced security features, and a simple interface for managing digital assetsOneKey Wallet
Manish Email Homepage
2024-8-30 17:02
Very well written content we appreciate your work, expecting more content like thisYard of Deals
grape krush strain Email Homepage
2024-9-3 05:28
I really liked the content of your blog. It is really amazing. Keep up the good work!grape krush strain
weed order canada Email Homepage
2024-9-4 18:48
https://herbapproach.com/product-category/weed-order-canada/Thanks, you have made a wonderful post. I love and appreciate your commitment.
Best streaming app Email Homepage
2024-9-4 20:13
Geoplixz+ anchors highly innovative technology for better user engagement. These include cutting-edge AI-driven content recommendations, high-definition format streaming, and a user-friendly interface that matches the tech-savvy expectations of your viewing audience. best streaming app
分页: 432/433 第一页 上页 427 428 429 430 431 432 433 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]