varnish加速网站
可能现在很多人还是用Squid来做代理。我测试了一下,使用Varnish来加速的效果比Squid的更理想。现在只是把服务跑了起来,还没有把日志分析程序以及报警相关的程序处理好。这几天处理好之后,我会把相关的资料更新。
版权所有,引用请先通知我!
下面是FreeBSD的优化配置文件。
/etc/sysctl.conf
- kern.ipc.nmbclusters=65536
- kern.ipc.somaxconn=16384
- kern.maxfiles=131072
- kern.maxfilesperproc=104856
- kern.threads.max_threads_per_proc=4096
- vfs.ufs.dirhash_maxmem=67108864
- kern.ipc.shmmax=134217728
- net.inet.tcp.msl=5000
/boot/loader.conf
- kern.ipc.maxsockets="131072"
- kern.ipc.maxpipekva="104857600"
内核文件里的SCHED_4BSD改为SCHED_ULE重新编译内核
上面的事情都处理好之后,接下来就是安装Varnish了。
- cd /usr/ports/www/varnish/
- make install clean
配置启动文件
/etc/rc.conf
- varnishd_enable="YES"
- varnishd_listen=:80
- varnishncsa_enable="YES"
/usr/local/etc/varnish/default.vcl
- #This is a basic VCL configuration file for varnish. See the vcl(7)
- #man page for details on VCL syntax and semantics.
- #
- #Default backend definition. Set this to point to your content
- #server.
- #
- backend jianblog {
- .host = "203.184.129.212";
- .port = "80";
- }
- #
- #Below is a commented-out copy of the default VCL logic. If you
- #redefine any of these subroutines, the built-in logic will be
- #appended to your code.
- #
- sub vcl_recv {
- set req.grace = 30s;
- if (req.request != "GET" &&
- req.request != "HEAD" &&
- req.request != "PUT" &&
- req.request != "POST" &&
- req.request != "TRACE" &&
- req.request != "OPTIONS" &&
- req.request != "DELETE") {
- /* Non-RFC2616 or CONNECT which is weird. */
- pipe;
- }
- if (req.http.host ~"^www.jianblog.com") {
- set req.backend = jianblog;
- } elsif (req.http.host ~"^jianblog.com") {
- set req.backend = jianblog;
- } elsif (req.http.host ~"^www.chinado.net") {
- set req.backend = jianblog;
- } elsif (req.http.host ~"^chinado.net") {
- set req.backend = jianblog;
- } else {
- error 404 "Unknown virtual host";
- }
- /* Always cache images and multimedia */
- if (req.url ~ "\.(jpg|jpeg|gif|png|tiff|tif|svg|swf|ico|mp3|mp4|m4a|ogg|mov|avi|wmv)$") {
- lookup;
- }
- /* Always cache CSS and javascript */
- if (req.url ~ "\.(css|js)$") {
- lookup;
- }
- /* Always cache static files */
- if (req.url ~ "\.(pdf|xls|vsd|doc|ppt|pps|vsd|doc|ppt|pps|xls|pdf|sxw|zip|gz|bz2|tgz|tar|rar|odc|odb|odf|odg|odi|odp|ods|odt|sxc|sxd|sxi|sxw|dmg|torrent|deb|msi|iso|rpm)$") {
- lookup;
- }
- if (req.request != "GET" && req.request != "HEAD") {
- /* We only deal with GET and HEAD by default */
- pipe;
- }
- if (req.http.Authorization || req.http.Cookie) {
- /* Not cacheable by default */
- pass;
- }
- lookup;
- }
- sub vcl_pipe {
- pipe;
- }
- sub vcl_pass {
- pass;
- }
- sub vcl_hash {
- set req.hash += req.url;
- if (req.http.host) {
- set req.hash += req.http.host;
- } else {
- set req.hash += server.ip;
- }
- hash;
- }
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 404 "Not in cache.";
- }
- if (!obj.cacheable) {
- pass;
- }
- deliver;
- }
- sub vcl_miss {
- fetch;
- }
- sub vcl_fetch {
- if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
- set obj.ttl = 3600s;
- } else {
- set obj.ttl = 30d;
- }
- if (!obj.cacheable) {
- pass;
- }
- if (obj.http.Set-Cookie) {
- pass;
- }
- set obj.grace = 30s;
- # set obj.prefetch = -30s;
- # deliver;
- }
- sub vcl_deliver {
- # deliver;
- }
- sub vcl_discard {
- /* XXX: Do not redefine vcl_discard{}, it is not yet supported */
- # discard;
- }
- sub vcl_prefetch {
- /* XXX: Do not redefine vcl_prefetch{}, it is not yet supported */
- # fetch;
- }
- sub vcl_timeout {
- /* XXX: Do not redefine vcl_timeout{}, it is not yet supported */
- # discard;
- }
- sub vcl_error {
- set obj.http.Content-Type = "text/html; charset=utf-8";
- synthetic {"
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html>
- <head>
- <title>"} obj.status " " obj.response {"</title>
- </head>
- <body>
- <h1>Error "} obj.status " " obj.response {"</h1>
- <p>"} obj.response {"</p>
- <h3>Guru Meditation:</h3>
- <p>XID: "} req.xid {"</p>
- <address><a href="http://www.jianblog.com/">Jian</a></address>
- </body>
- </html>
- "};
- deliver;
- }
这两天,我会把日志分析的更新过来。
freeke said,
十二月 10, 2008 at 9:00 上午
Hey 博主,不好意思我拿了你的配置本地测试一下,可是我在访问页面时超级慢,不知道如何分析呢,我加了你MSN,估计你应该比较忙,就没敢打扰你啊!我在CU发了个贴,描述了一下我的情况,要是有空帮我分析分析吧?在此感谢啦!
http://bbs.chinaunix.net/thread-1329583-1-1.html