搜档网
当前位置:搜档网 › 架构 Varnish+nginx+php(FastCGI)+MYSQL5+MenCache+MenC实现php大负载

架构 Varnish+nginx+php(FastCGI)+MYSQL5+MenCache+MenC实现php大负载

架构 Varnish+nginx+php(FastCGI)+MYSQL5+MenCache+MenC
利用 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:
下载地址:
https://www.sodocs.net/doc/ac14869666.html,
...
第二个压缩包:nginx_php_mysql_1.0_2of2.zip:
下载地址:
https://www.sodocs.net/doc/ac14869666.html,
...
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.co...
2.)安装Nginx所需的pcre库:
引用
[root@localhost]#tar zxvf pcre-7.2.tar.gz
[root@localhost]#cd pcre-7.2/
[root@localhost]#./configure
[root@localhost]#make && make install
[root@localhost]#c

d ../
3.)Nginx的编译参数如下:
引用
[root@localhost]#./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 文件,因此我稍微变通了一下:
引用
[root@localhost]#mkdir /usr/include/pcre/.libs/
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/https://www.sodocs.net/doc/ac14869666.html,
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a
[root@localhost]#cp /usr/local/lib/libpcre.a /usr/include/pcre/https://www.sodocs.net/doc/ac14869666.html,
然后,修改 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
https://www.sodocs.net/doc/ac14869666.html,
;
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 运行状态
输入地址
http://192.168.0.1/NginxSt
...,输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
这里我是用虚拟机做的测试,所以链接数比较少.
第一行表示目前活跃的连接数
第三行的第三个数字表示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
允许使用汇编模式(优化性能)
引用
[root@localhost]#tar zxvf mysql-5.0.45.tar.gz
[root@localhost]#./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
[root@localhost]#make -j 50 && make install
[root@localhost]#groupadd mysql
[root@localhost]#useradd -g mysql mysql
[root@localhost]#bin/mysql_install_db --user=mysql
[root@lo

calhost]#chown -R root .
[root@localhost]#chown -R mysql data
[root@localhost]#chgrp -R mysql .
[root@localhost]#bin/mysqld_safe --user=mysql &
[root@localhost]#cd bin
[root@localhost]#/usr/local/mysql/share/mysql/mysql.server stop
[root@localhost]#cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
[root@localhost]#chmod 755 /etc/init.d/mysql
[root@localhost]#chkconfig --level 345 mysql on
-------------------------------------------------------------------------------------------------------------------------
三、)安装PHP 5.2.5(FastCGI模式)
1、编译安装PHP 5.2.5所需的支持库:
引用
[root@localhost]#tar zxvf libiconv-1.11.tar.gz
[root@localhost]#cd libiconv-1.11/
[root@localhost]#./configure --prefix=/usr/local/webserver/lib/libiconv
[root@localhost]#make && make install
[root@localhost]#cd ../
[root@localhost]#tar zxvf freetype-2.3.5.tar.gz
[root@localhost]#cd freetype-2.3.5/
[root@localhost]#./configure --prefix=/usr/local/webserver/lib/freetype
[root@localhost]#make && make install
[root@localhost]#cd ../
[root@localhost]#tar zxvf libpng-1.2.20.tar.gz
[root@localhost]#cd libpng-1.2.20/
[root@localhost]#./configure
[root@localhost]#make && make install
[root@localhost]#cd ../
[root@localhost]#tar zxvf jpegsrc.v6b.tar.gz
[root@localhost]#cd jpeg-6b/
[root@localhost]#./configure --enable-static --enable-shared
[root@localhost]#make && make install
[root@localhost]#cd ../
t[root@localhost]#ar zxvf gd-2.0.35.tar.gz
[root@localhost]#cd gd-2.0.35/
[root@localhost]#./configure --prefix=/usr/local/webserver/lib/gd --with-freetype=/usr/local/webserver/lib/freetype --with-jpeg --with-png
[root@localhost]#make
[root@localhost]#make install
[root@localhost]#cd ../
[root@localhost]#tar zxvf libxml2-sources-2.6.30.tar.gz
[root@localhost]#cd libxml2-2.6.30/
[root@localhost]#./configure --prefix=/usr/local/webserver/lib/libxml
[root@localhost]#make && make install
[root@localhost]#cd ../
-------------------------------------------------------------------------------------------------------------
2、编译安装PHP(FastCGI模式)
引用
[root@localhost]#tar zxvf php-5.2.4.tar.gz
[root@localhost]#cd php-5.2.4/
[root@localhost]#./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 --e

nable-mbstring=all --with-ming=/usr --with-pdo-sqlite --enable-pdo --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config
[root@localhost]#cp php.ini-dist /usr/local/php/etc/php.ini
[root@localhost]#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文件的修改:
引用
[root@localhost]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添加扩展的方法。(本步骤可选)
引用
[root@localhost]#cd php-5.2.4/pcntl
[root@localhost]#/usr/local/php/bin/phpize
[root@localhost]#./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost]#make && make install
[root@localhost]#cd ../../../
[root@localhost]#cd php-5.2.5/ext/gd/
[root@localhost]#/usr/local/php/bin/phpize
[root@localhost]#./configure --with-jpeg-dir --with-png-dir --with-zlib-dir --with-ttf --with-freetype-dir --with-php-config=/usr/local/php/bin/php-config
[root@localhost]#make -j 50
[root@localhost]#make install
利用 Memcached 处理实时数据读写;MySQL是影响性能的最大瓶颈,可以用一台MySQL主库(只写)+多台MySQL辅库(只读)的主辅库集群来解决。另外,访问计数等实时性很强的东西用Memcache做缓存。

引用
[root@localhost]#tar zxvf memcache-2.2.1.tgz
[root@localhost]#cd memcache-2.2.1/
[root@localhost]#/usr/local/php/bin/phpize
[root@localhost]#./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost]#make -j 50
[root@localhost]#make install

安装支持MYSQL PDO驱动
引用
[root@localhost]#tar xzvf PDO-1\[1\].0.3.tgz
[root@localhost]#cd PDO-1.0.3/
[root@localhost]#/usr/local/php/bin/phpize
[root@localhost]#./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql/
[root@localhost]#make -j 50
[root@localhost]#make install
-------------------------------------------------------------------------------------------------------------
6.安装lighttpd中附带的spawn-fcgi,用来启动php-cgi
 注:压缩包中的spawn-fcgi程序为已经编译成二进制的版本。
[root@localhost]#cp spawn-fcgi /usr/local//php/bin
[root@localhost]#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.设定开机启动
引用
[root@localhost]#echo "/usr/local/mysql/share/mysql/mysql.server start" >> /etc/rc.local
[root@localhost]#echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local
[root@localhost]#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
-----------------------------------------------------------------------------------------------------------------------
如果出现 php的“No input file specified.”
首先php.ini的配置中
cgi.fix_pathinfo=1
doc_root=
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
1.编译安装varnish:
[root@localhost]#tar zxvf varnish-1.1.2.tar.gz
[root@localhost]#cd varnish-1.1.2
[root@localhost]#./configure --prefix=/usr/local/varnish --enable-debugging-symbols --enable-developer-warnings --enable-dependency-tracking
[root@localhost]#make && make install
2.创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):
[root@localhost]#/usr/sbin/groupadd www
[root@localhost]#/usr/sbin/useradd -g www www
[root@localhost]#mkdir -p /usr/local/varnish/var/varnish/
[root@localhost]#chmod +w /usr/local/varnish/var/varnish/
[root@localhost]#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 ~ "^https://www.sodocs.net/doc/ac14869666.html,") {
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对域名为
https://www.sodocs.net/doc/ac14869666.html,
的请求进行处理,非
https://www.sodocs.net/doc/ac14869666.html,
域名的请求则返回"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
https://www.sodocs.net/doc/ac14869666.html,e
vcl.discard
vcl.list
vcl.show
param.show [-l] []
param.set
help [command]
url.purge
dump.pool
3、通过Varnish管理端口,使用正则表达式批量清除缓存:
(1)、例:清除类似
https://www.sodocs.net/doc/ac14869666.html,/a/quans
...的URL地址):
/usr/local/varnish/bin/varnishadm -T 127.0.0.1:3500 url.purge /a/
(2)、例:清除类似
https://www.sodocs.net/doc/ac14869666.html,/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缓存同样可以使用该函数,无需作任何修改,十分方便):
function purge($ip, $url)
{
$errstr = '';
$errno = '';
$fp = fsockopen ($ip, 80, $errno, $errstr, 2);
if (!$fp)
{
return false;
}
else
{
$out = "PURGE $url HTTP/1.1\r\n";
$out .= "Host:https://www.sodocs.net/doc/ac14869666.html,\r\n";
$out .= "Connection: close\r\n\r\n";
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
---------------------------------------------------------------------------------------------------------------------------
五.)Memcachedb
Memcachedb:编译以及安装
说明: memcachedb跟memcache一样,网络socket数据处理依赖于libevent,所以,在安装之前需要下载三个安装包,即libevent、Berkeley Db以及memcachedb。
Libevent 下载页面:
https://www.sodocs.net/doc/ac14869666.html,/~provos/
... ,下载最新稳定版本就行。
Berkeley Db下载页面:
https://www.sodocs.net/doc/ac14869666.html,/tech
... 需要安装4.6版本
memcachedb下载页面:
https://www.sodocs.net/doc/ac14869666.html,/p/m
...下载最新版本 0.1.0版本
按照顺序安装,Libevent和Berkeley Db按照常规安装即可,这里以Linux 环境为标准。
[======Libevent=====]
[root@localhost]#tar -zxvf libevent-1.3e.tar.gz
[root@localhost]#cd libevent-1.3e
[root@localhost]#./configure
[root@localhost]#make && make install
[======Berkeley Db=====]
[root@localhost]#tar -zxvf db-4.6.19.tar.gz
#需要进入特定操作系统编译环境,更常规软件的编译有些区别
[root@localhost]#cd db-4.6.19/build_unix/
#然后才能够开始编译
[root@localhost]#../dist/configure
[root@localhost]#make && make install
#如果没有指定特殊安装路径,编译完成,需要将Berkeley Db运行库的路径添加到系统配置里面
[root@localhost]#echo "/usr/local/BerkeleyDB.4.6/lib/" >> /etc/ld.so.conf
#重载系统Ld运行库
[root@localhost]#ldconfig
[======Memcachedb=====]
[root@localhost]#tar -zxvf memcachedb-0.1.0.tar.gz
[root@localhost]#cd memcachedb-0.1.0
#如果您要存储的内容的键值的长度和单笔内容的长度有变化,您需要更改一个文件 memcachedb.h
[root@localhost]#vi memcachedb.h
////////////////////////////////////////////////////////////
#define END_LEN 32 ----> 这是是 Key+Value 的最大长度
#define END_LEN_STR "31" ----> 这是是 key 的最大长度
/////////////////////////////////////////////////////////
#默认key键值只预留了31个字节的长度,单笔数据最长是32个字节数。
#例如您如果需要用这个玩艺来存储文本内容数据,假设您的键值是一个32个字节的md5,单笔数据允许最长10K,那您可以设定
////////////////////////////////////////////////////////////
#define END_LEN 102

400 ----> 这是是 Key+Value 的最大长度
#define END_LEN_STR "32" ----> 这是是key的最大长度
/////////////////////////////////////////////////////////
#修改完之后,直接make就行
[root@localhost]#make
#编译完之后,将生成可执行memcachedb拷贝到 /usr/sbin/
[root@localhost]#cp -f memcachedb /usr/sbin/
#启动 memcachedb
#第一种,读写合并模式,即不做辅拷贝,即读又写,不做冗余备份
[root@localhost]#memcachedb -p21211 -d -r -u root -H /data/mdb_11211 -N
#第二种,读写分离/备份模式
#主服务器 192.168.0.1 读写监听 11211 ,同步通过31211的端口完成,不占用繁忙的主服务端口
[root@localhost]#memcachedb -p21211 -d -r -u root -H /data/mdb_11211_m -N -R 127.0.0.1:31211 -M
# 辅服务器 192.168.0.2 只读监听 21212,从192.168.0.1 的 31211端口同步数据,同时再开一个31212端口对外提供数据同步服务
[root@localhost]#memcachedb -p21212 -d -r -u root -H /data/mdb_11211_from_16801_s -O 192.168.0.1:31211 -R 192.168.0.2:31212 -S

相关主题