2013-11-19 11:35:01
来 源
itjs.cn
Varnish
本文介绍Varnish+Nginx服务器配置,希望对于Varnish入门者给与帮助。更多Varnish资源请本站内搜索。

说明:

我在设计系统架构时,进行了大胆的尝试,只用6台Web服务器,达到了可承受4000万PV(页面访问量)的性能:

抛弃了 Apache,因为它能承受的并发连接相对较低;

抛弃了 Squid,因为它在内存利用、访问速度、并发连接、清除缓存等方面不如 Varnish;

抛弃了 PHP4,因为 PHP5 处理面向对象代码的速度要比 PHP4 快,另外,PHP4 已经不再继续开发;

抛弃了 F5 BIG-IP 负载均衡交换机,F5 虽然是个好东西,但由于价格不菲,多个部门多个产品都运行在其之上,流量大、负载高,从而导致性能大打折扣;

利用 Varnish cache 减少了90%的数据库查询,解决了MySQL数据库瓶颈;

利用 Varnish cache 的内存缓存命中加快了网页的访问速度;

利用 Nginx + PHP5(FastCGI) 的胜过Apache 10倍的高并发性能,以最少的服务器数量解决了PHP动态程序访问问题;

利用 Memcached 处理实时数据读写;

利用 HAProxy 做接口服务器健康检查;

经过压力测试,每台Web服务器能够处理3万并发连接数,承受4千万PV完全没问题。

保证4千万PV的并发连接数:(40000000PV / 86400秒 * 10个派生连接数 * 5秒内响应 * 5倍峰值) / 6台Web服务器 = 19290连接数

实验证明: 

举个简单的例子,服务器192.168.0.2上运行Nginx+PHP,192.168.0.3上运行Apache+PHP,你在 192.168.0.4上安装压力测试工具webbench,以30万并发连接分别请求Nginx和Apache服务器上的一个PHP文件60秒钟。在这 期间,你用你的浏览器访问Apache服务器上的PHP文件,会发现要么是“该页无法显示”、要么是等待好几秒钟才能打开,而Nginx服务器的PHP文 件,依然没有丝毫影响,访问速度仍然飞快。

webbench -c 300000 -t 60 http://192.168.0.2/index.php

webbench -c 300000 -t 60 http://192.168.0.3/index.php

以下为 Nginx 0.5.33 + PHP 5.2.5 (FastCGI) 服务器在3万并发连接下,开启的10个Nginx进程和250个php-cgi进程时的系统负载情况:

-------------------------------------------------------------------------

安装步骤:

(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为AS4.3)

一、获取相关开源程序:

1、下载程序源码包到当前目录:

本文中提到的所有开源软件为截止到2007年9月21日的最新稳定版。我将它们打了两个压缩包。

第一个压缩包:nginx_php_mysql_1.0_1of2.zip:

下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289607

第二个压缩包:nginx_php_mysql_1.0_2of2.zip:

下载地址:http://ishare.iask.sina.com.cn/cgi-bin/fileid.cgi?fileid=2289595

2、解压缩:

unzip nginx_php_mysql_1.0_1of2.zip

unzip nginx_php_mysql_1.0_2of2.zip

-------------------------------------------------------------------------

一、) 安装Nginx

1.) 安装

Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。欢迎访问 Nginx 的中文维基,http://wiki.codemongers.com/NginxChs.

2.)安装Nginx所需的pcre库:

[[email protected]]#tar zxvf pcre-7.2.tar.gz

[[email protected]]#cd pcre-7.2/

[[email protected]]#./configure

[[email protected]]#make && make install

[[email protected]]#cd ../

3.)Nginx的编译参数如下:

[[email protected]]#./configure --user=www --group=www --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl --with-pcre=/usr/local/lib --with-http_stub_status_module --without-http_memcached_module --without-http_fastcgi_module --without-http_rewrite_module --without-http_map_module --without-http_geo_module --without-http_autoindex_module

在这里,需要说明一下,由于Nginx的配置文件中我想用到正则,所以需要 pcre 模块的支持。上面安装步骤里我已经安装了 pcre 及 pcre-devel 的rpm包,但是 Ngxin 并不能正确找到 .h/.so/.a/.la 文件,因此我稍微变通了一下:

[[email protected]]#mkdir /usr/include/pcre/.libs/

[[email protected]]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a

[[email protected]]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la

[[email protected]]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a

[[email protected]]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.la

然后,修改 objs/Makefile 大概在908行的位置上,注释掉以下内容:

./configure --disable-shared

接下来,就可以正常执行 make 及 make install 了。

4.) 修改配置文件 /usr/local/server/nginx/conf/nginx.conf

以下是我的 nginx.conf 内容,仅供参考:

#运行用户

user nobody nobody;

#启动进程

worker_processes 2;

#全局错误日志及PID文件

error_log logs/error.log notice;

pid logs/nginx.pid;

#工作模式及连接数上限

events {

use epoll;

worker_connections 1024;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

#设定mime类型

include conf/mime.types;

default_type application/octet-stream;

#设定日志格式

log_format main '$remote_addr - $remote_user [$time_local] '

'"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

'"$gzip_ratio"';

log_format download '$remote_addr - $remote_user [$time_local] '

'"$request" $status $bytes_sent '

'"$http_referer" "$http_user_agent" '

'"$http_range" "$sent_http_content_range"';

#设定请求缓冲

client_header_buffer_size 1k;

large_client_header_buffers 4 4k;

#开启gzip模块

gzip on;

gzip_min_length 1100;

gzip_buffers 4 8k;

gzip_types text/plain;

output_buffers 1 32k;

postpone_output 1460;

#设定access log

access_log logs/access.log main;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 65;

#设定负载均衡的服务器列表

upstream mysvr {

#weigth参数表示权值,权值越高被分配到的几率越大

#本机上的Squid开启3128端口

server 192.168.8.1:3128 weight=5;

server 192.168.8.2:80 weight=1;

server 192.168.8.3:80 weight=6;

}

#设定虚拟主机

server {

listen 80;

server_name 192.168.0.1 www.test.com;

charset gb2312;

#设定本虚拟主机的访问日志

#access_log logs/access.log main;

#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid

#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好

location ~ ^/(img|js|css)/ {

root /home/web;

expires 24h;

}

#对 "/" 启用负载均衡

location / {

proxy_pass http://mysvr;

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;

client_body_buffer_size 128k;

proxy_connect_timeout 90;

proxy_send_timeout 90;

proxy_read_timeout 90;

proxy_buffer_size 4k;

proxy_buffers 4 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

}

#设定查看Nginx状态的地址

location /NginxStatus {

stub_status on;

access_log on;

auth_basic "NginxStatus";

auth_basic_user_file conf/htpasswd;

}

}

}

运行以下命令检测配置文件是否无误:

如果没有报错,那么就可以开始运行Nginx了,执行以下命令即可:

备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:

5.) 查看 Nginx 运行状态

Varnish + nginx - darling - 向凌锋 的博客 输入地址 http://192.168.0.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:

Active connections: 328

server accepts handled requests

9309 8982 28890

Reading: 1 Writing: 3 Waiting: 324

Varnish + nginx - darling - 向凌锋 的博客这里我是用虚拟机做的测试,所以链接数比较少.

第一行表示目前活跃的连接数

第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。

top -b -nl

查看NGINX的进程号:

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

6.)配置支持FCGI文件

vi /usr/local/webserver/nginx/conf/fcgi.conf

输入以下内容:

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_URI $document_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

#fastcgi_param REDIRECT_STATUS 200;

7.)启动Nginx

ulimit -SHn 51200

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

-------------------------------------------------------------------------

8.)配置开机自动启动Nginx + PHP

vi /etc/rc.local

在末尾增加以下内容:

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/php/bin/php-cgi

ulimit -SHn 51200

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

-------------------------------------------------------------------------

二.) 安装MYSQL

安装mysql-5.0.45.tar.gz, 下面是总体的编译文件

1. -static 13%

--with-client-ldflags=-all-static

--with-mysqld-ldflags=-all-static

静态链接提高13%性能

2. -pgcc 1%

CFLAGS="-O3 -mpentiumpro -mstack-align-double" CXX=gcc

CXXFLAGS="-O3 -mpentiumpro -mstack-align-double

-felide-constructors -fno-exceptions -fno-rtti"

如果是Inter处理器,使用pgcc提高1%性能

3. Unix Socket 7.5%

--with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock

使用unix套接字链接提高7.5%性能,所以在windows下mysql性能肯定不如unix下面

4. --enable-assembler

允许使用汇编模式(优化性能)

[[email protected]]#tar zxvf mysql-5.0.45.tar.gz

[[email protected]]#./configure --prefix=/usr/local/mysql/ --without-debug --with-unix-socket-path=/tmp/mysql.sock --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client

[[email protected]]#make -j 50 && make install

[[email protected]]#groupadd mysql 

[[email protected]]#useradd -g mysql mysql 

[[email protected]]#bin/mysql_install_db --user=mysql 

[[email protected]]#chown -R root . 

[[email protected]]#chown -R mysql data 

[[email protected]]#chgrp -R mysql . 

[[email protected]]#bin/mysqld_safe --user=mysql & 

[[email protected]]#cd bin 

[[email protected]]#/usr/local/mysql/share/mysql/mysql.server stop 

[[email protected]]#cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql

[[email protected]]#chmod 755 /etc/init.d/mysql

[[email protected]]#chkconfig --level 345 mysql on

-------------------------------------------------------------------------------------------------------------------------

三、)安装PHP 5.2.5(FastCGI模式)

1、编译安装PHP 5.2.5所需的支持库:

[[email protected]]#tar zxvf libiconv-1.11.tar.gz

[[email protected]]#cd libiconv-1.11/

[[email protected]]#./configure --prefix=/usr/local/webserver/lib/libiconv

[[email protected]]#make && make install

[[email protected]]#cd ../

[[email protected]]#tar zxvf freetype-2.3.5.tar.gz

[[email protected]]#cd freetype-2.3.5/

[[email protected]]#./configure --prefix=/usr/local/webserver/lib/freetype

[[email protected]]#make && make install

[[email protected]]#cd ../

[[email protected]]#tar zxvf libpng-1.2.20.tar.gz

[[email protected]]#cd libpng-1.2.20/

[[email protected]]#./configure

[[email protected]]#make && make install

[[email protected]]#cd ../

[[email protected]]#tar zxvf jpegsrc.v6b.tar.gz

[[email protected]]#cd jpeg-6b/

[[email protected]]#./configure --enable-static --enable-shared

[[email protected]]#make && make install

[[email protected]]#cd ../

t[[email protected]]#ar zxvf gd-2.0.35.tar.gz

[[email protected]]#cd gd-2.0.35/

[[email protected]]#./configure --prefix=/usr/local/webserver/lib/gd --with-freetype=/usr/local/webserver/lib/freetype --with-jpeg --with-png

[[email protected]]#make

[[email protected]]#make install

[[email protected]]#cd ../

[[email protected]]#tar zxvf libxml2-sources-2.6.30.tar.gz

[[email protected]]#cd libxml2-2.6.30/

[[email protected]]#./configure --prefix=/usr/local/webserver/lib/libxml

[[email protected]]#make && make install

[[email protected]]#cd ../

-------------------------------------------------------------------------------------------------------------

2、编译安装PHP(FastCGI模式)

[[email protected]]#tar zxvf php-5.2.4.tar.gz

[[email protected]]#cd php-5.2.4/

[[email protected]]#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --enable-ftp --with-config-file-path=/usr/local/php --enable-zip --with-zlib --with-curl --without-iconv --with-iconv=/usr/local/lib --with-libxml-dir=/usr --enable-xml --with-xmlrpc --enable-mbregex-backtrack --with-gettext --with-gd=/usr/lib --enable-gd-native-ttf --with-ttf --enable-gd-jis-conv --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-freetype-dir=/usr/local/php/lib/freetype --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-dom --enable-safe-mode --enable-discard-path --disable-debug --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --with-ldap --enable-sockets --enable-soap --enable-inline-optimization --enable-mbstring=all --with-ming=/usr --with-pdo-sqlite --enable-pdo --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config

[[email protected]]#cp php.ini-dist /usr/local/php/etc/php.ini

[[email protected]]#cd ../

-----------------------------------------------------------------------------------------------------------------

3.修改php.ini文件

手工修改:查找/usr/local/php/etc/php.ini中的extension_dir = "./"

修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

并在此行后增加以下几行,然后保存:

extension = "memcache.so"

extension = "gd.so"

4.自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:

[[email protected]]sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"nextension = "memcache.so"nextension = "gd.so"n#' /usr/local/php/etc/php.ini

--------------------------------------------------------------------------------

5.附:编译PHP之后,为PHP添加扩展的方法。(本步骤可选)

[[email protected]]#cd php-5.2.4/pcntl

[[email protected]]#/usr/local/php/bin/phpize

[[email protected]]#./configure --with-php-config=/usr/local/php/bin/php-config

[[email protected]]#make && make install

[[email protected]]#cd ../../../

[[email protected]]#cd php-5.2.5/ext/gd/ 

[[email protected]]#/usr/local/php/bin/phpize 

[[email protected]]#./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --with-php-config=/usr/local/php/bin/php-config 

[[email protected]]#make -j 50 

[[email protected]]#make install

利用 Memcached 处理实时数据读写;MySQL是影响性能的最大瓶颈,可以用一台MySQL主库(只写)+多台MySQL辅库(只读)的主辅库集群来解决。另外,访问计数等实时性很强的东西用Memcache做缓存。 

[[email protected]]#tar zxvf memcache-2.2.1.tgz 

[[email protected]]#cd memcache-2.2.1/ 

[[email protected]]#/usr/local/php/bin/phpize 

[[email protected]]#./configure --with-php-config=/usr/local/php/bin/php-config 

[[email protected]]#make -j 50 

[[email protected]]#make install 

安装支持MYSQL PDO驱动

[[email protected]]#tar xzvf PDO-1[1].0.3.tgz 

[[email protected]]#cd PDO-1.0.3/ 

[[email protected]]#/usr/local/php/bin/phpize 

[[email protected]]#./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql/ 

[[email protected]]#make -j 50 

[[email protected]]#make install

-------------------------------------------------------------------------------------------------------------

6.安装lighttpd中附带的spawn-fcgi,用来启动php-cgi

注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。

[[email protected]]#cp spawn-fcgi /usr/local//php/bin

[[email protected]]#chmod +x /usr/local/php/bin/spawn-fcgi

7、启动php-cgi进程,监听127.0.0.1的10080端口,进程数为250(如果服务器内存小于3GB,可以只开启25个进程),用户为www:

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/php/bin/php-cgi

8.设定开机启动

[[email protected]]#echo "/usr/local/mysql/share/mysql/mysql.server start" >> /etc/rc.local

[[email protected]]#echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local

[[email protected]]#echo "/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 250 -u www -f /usr/local/php/bin/php-cgi" >> /etc/rc.local

四、优化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 使配置立即生效:

/sbin/sysctl -p

-----------------------------------------------------------------------------------------------------------------------

附:最近经常搞lighttpd+fastcgi+php或者nginx+fastcgi+php,时常被php的“No input file specified.”给郁闷了,把我遇到的情况说一下

首先php.ini的配置中

cgi.fix_pathinfo=1

doc_root=

doc_root曾经被我设置过一个路径,结果php老提示“No input file specified.”,只有一个虚拟机好使。改掉后就正常了。

nginx中的配置有些麻烦

fastcgi_pass 127.0.0.1:1234;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;

每个虚机要根据自己不通的虚机设置不能的目录,要保证这个路径正确。

fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;不能在fastcgi_pass 127.0.0.1:1234;的前面。

记得修改了php.ini要重启fastcgi服务。

其实都是因为粗心造成的,本来很简单,写出来也给自己提个醒

---------------------------------------------------------------------------------------------------------------------------

四.)安装Varnish

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

在我看来,使用Varnish代替Squid的理由有三点:

1、Varnish采用了“Visual Page Cache”技术,在内存的利用上,Varnish比Squid具有优势,它避免了Squid频繁在内存、磁盘中交换文件,性能要比Squid高。

2、Varnish的稳定性还不错,顺便说一句,Varnish的性能的发挥关键在于Varnish配置文档的优化.

3、通过Varnish管理端口,可以使用正则表达式快速、批量地清除部分缓存,这一点是Squid不能具备的

4. 还有一点,应该算是Varnish的缺点了吧,就是Varnish的缓存基本上在内存中,如果Varnish进程停止再启动,Varnish就会重新访问 后端Web服务器,再一次进行缓存.虽然Squid性能没有Varnish高,但它停止、重启的时候,可以直接先从磁盘读取缓存数据。

varnish是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang (http://www.vg.no) 使用3台Varnish代替了原来的12台squid,性能比以前更好。

varnish的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多。在1975年时,储存媒介只有两种:内存与硬盘。但现在计算机系统的内存除了主存外,还包括了cpu内的L1、L2,甚至有L3快取。硬盘上也有自己的快取装置,因此squid cache自行处理物件替换的架构不可能得知这些情况而做到最佳化,但操作系统可以得知这些情况,所以这部份的工作应该交给操作系统处理,这就是 Varnish cache设计架构.

------------------------------------------------------------------------------------------------------------------

1.编译安装varnish:

下载源码包链接: wget http://nchc.dl.sourceforge.net/sourceforge/varnish/varnish-1.1.1.tar.gz

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

[[email protected]]#wget http://blog.s135.com/soft/linux/varnish/varnish-1.1.2.tar.gz

[[email protected]]#tar zxvf varnish-1.1.2.tar.gz

[[email protected]]#cd varnish-1.1.2

[[email protected]]#./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking

[[email protected]]#make && make install

2.创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):

[[email protected]]#/usr/sbin/groupadd www

[[email protected]]#/usr/sbin/useradd -g www www

[[email protected]]#mkdir -p /usr/local/varnish/var/varnish/

[[email protected]]#chmod +w /usr/local/varnish/var/varnish/

[[email protected]]#chown -R www:www /usr/local/varnish/var/varnish/

3.创建Varnish日志目录(/usr/local/varnish/logs):

[root@localhost]#mkdir -p /usr/local/varnish/logs

[root@localhost]#chmod +w /usr/local/varnish/logs

[root@localhost]#chown -R www:www /usr/local/varnish/logs

[root@localhost]#touch /usr/local/varnish/logs/varnish.log

4.创建Varnish配置文件:

[root@localhost]#vi /usr/local/varnish/vcl.conf

backend myblogserver {

set backend.host = "192.168.0.1";

set backend.port = "80";

}

acl purge {

"localhost";

"127.0.0.1";

"192.168.0.0"/24;

"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 ~ "^www.test.com") {

set req.backend = myblogserver;

if (req.request != "GET" && req.request != "HEAD") {

pipe;

}

else {

lookup;

}

}

else {

error 404 "Zhang Quan Sheng 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.1,端口为80的web服务器;

(2)、Varnish允许localhost、127.0.0.1、192.168.0.***三个来源IP通过PURGE方法清除缓存;

(3)、Varnish对域名为www.test.com的请求进行处理,非www.test.com域名的请求则返回"Zhang Quan Sheng Cache Server";

(4)、Varnish对HTTP协议中的GET、HEAD请求进行缓存,对POST请求透过,让其直接访问后端Web服务器。之所以这样配置,是因为POST请求一般是发送数据给服务器的,需要服务器接收、处理,所以不缓存;

(5)、Varnish对以.txt和.js结尾的URL缓存时间设置1小时,对其他的URL缓存时间设置为30天。

-------------------------------------------------------------------------------------------------------------------------

5、启动Varnish

ulimit -SHn 51200l

/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file, /usr/local/varnish/var/varnish/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 /usr/local/varnish/var/varnish/ -w /usr/local/varnish/var/varnish/varnish.log &

7.配置开机自动启动Varnish

vi /etc/rc.local

ulimit -SHn 51200l

/usr/local/varnish/sbin/varnishd -n /var/vcache -f /usr/local/varnish/vcl.conf -a 0.0.0.0:80 -s file, /usr/local/varnish/var/varnish/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 /usr/local/varnish/var/varnish/ -w /usr/local/varnish/var/varnish/varnish.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://www.k18.com/a/quansheng.html的URL地址):

/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/

(2)、例:清除类似http://blog.s135.com/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缓存同样可以使用该函数,无需作任何修改,十分方便):

<?php 

function purge($ip, $url) 

$errstr = ''; 

$errno = ''; 

$fp = fsockopen ($ip, 80, $errno, $errstr, 2); 

if (!$fp) 

return false; 

else 

$out = "PURGE $url HTTP/1.1rn"; 

$out .= "Host:blog.s135.comrn"; 

$out .= "Connection: closernrn"; 

fputs ($fp, $out); 

$out = fgets($fp , 4096); 

fclose ($fp); 

return true; 

purge("192.168.0.4", "/index.php"); 

?>

附2:2007年12月10日,我写了一个每天0点运行,按天切割Varnish日志,生成一个压缩文件,同时删除上个月旧日志的脚本(/usr/local/varnish/var/varnish/cutlog.sh):

/usr/local/varnish/var/varnish/cutlog.sh文件内容如下:

#!/bin/sh

# This file run at 00:00

date=$(date -d "yesterday" +"%Y-%m-%d")

pkill -9 varnishncsa

mv /usr/local/varnish/var/varnish/youvideo.log /usr/local/varnish/var/varnish/${date}.log

/usr/local/varnish/bin/varnishncsa -n /usr/local/varnish/var/varnish/ -w /usr/local/varnish/var/varnish/youvideo.log &

mkdir -p /usr/local/varnish/var/varnish/youvideo/

gzip -c /usr/local/varnish/var/varnish/${date}.log > /usr/local/varnish/var/varnish/${date}.log.gz

rm -f /usr/local/varnish/var/varnish/${date}.log

rm -f /usr/local/varnish/var/varnish/youvideo/$(date -d "-1 month" +"%Y-%m*").log.gz

设置在每天00:00定时执行:

/usr/bin/crontab -e

或者  

vi /var/spool/cron/root

输入以下内容:

0 0 * * * /bin/sh /usr/local/varnish/var/varnish/cutlog.sh

--------------------------------------------------------------------------------------------------------------------------

附3.TCP连接数Varnish要比Squid少,因为Varnish的TCP连接释放要比Squid快。

但同时处理的请求数Varnish要比Squid高一些,一台Varnish、另一台Squid,分给它们的连接

数相同,Varnish实时处理的请求数比Squid多1倍,平均处理的请求数也比Squid多100余个:

/usr/local/webserver/varnish/bin/varnishstat

-----------------------------------------------------------

70979868 580.97 356.55 Client requests received

70897998 580.97 356.14 Cache hits

/usr/local/squid/bin/squidclient -p 80 mgr:5min

-----------------------------------------------------------

client_http.requests = 248.425264/sec

client_http.hits = 245.135282/sec

如果正常的话,vcache这个目录里只有一个大小为1G的文件:varnish_cache.data

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。