热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Varnish3.0安装配置过程笔记

Varnish有centos下的rpm和ubuntu下的deb二进制包下载,但我选择的是源码编译安装先下载wgethttp://repo.varnish-cache.org/source/varnish-3.0.0.tar.gztar-xzvfvarnish-3.0.0.tar.gzyuminstallgccyumin

Varnish有centos下的rpm和ubuntu下的deb二进制包下载,但我选择的是源码编译安装

先下载 wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz

tar -xzvf varnish-3.0.0.tar.gz

 

yum install gcc

yum install gcc-c++ libstdc++-devel

yum install -y httpd-devel pcre perl pcre-devel zlib zlib-devel GeoIP GeoIP-devel

 

Varnish需要pcre支持,如果服务器没有安装pcre-devel会提示需要指定prce目录。
Centos服务器上使用yum install pcre-devel安装

 

解压varnish进行编译安装

cd varnish-3.0.0

./autogen.sh

./configure --prefix=/usr/local/varnish PKG_CONFIG_PATH=/usr/lib/pkgconfig
 

make

make install

安装成功后,安装的目录是

cd /usr/local/varnish/sbin

./varnishd -V

查看版本,看是否安装成功

测试varnish

先编辑配置文件

vi /usr/local/varnish/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 default {
     .host = "192.168.88.156";
     .port = "80";
     ###下面三行为新加配
     .connect_timeout = 1s;
     .first_byte_timeout = 5s;
     .between_bytes_timeout = 2s;
 }
#
# 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 {
     if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For =
                req.http.X-Forwarded-For + ", " + client.ip;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
     }
     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. */
         return (pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
     if (req.http.Authorization || req.http.COOKIE) {
         /* Not cacheable by default */
         return (pass);
     }
     return (lookup);
 }
#
 sub vcl_pipe {
#     # Note that only the first request to the backend will have
#     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
#     # have it set for all requests, make sure to have:
#     # set bereq.http.connection = "close";
#     # here.  It is not set by default as it might break some broken web
#     # applications, like IIS with NTLM authentication.
     return (pipe);
 }
#
 sub vcl_pass {
     return (pass);
 }
#
 sub vcl_hash {
     hash_data(req.url);
     if (req.http.host) {
         hash_data(req.http.host);
     } else {
         hash_data(server.ip);
     }
     return (hash);
 }
#
 sub vcl_hit {
     return (deliver);
 }
#
 sub vcl_miss {
     return (fetch);
 }
#
 sub vcl_fetch {
     if (beresp.ttl <= 0s ||
         beresp.http.Set-COOKIE ||
         beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 120 s;
                return (hit_for_pass);
     }
     return (deliver);
 }
#
 sub vcl_deliver {
     return (deliver);
 }
#
# sub vcl_error {
#     set obj.http.Content-Type = "text/html; charset=utf-8";
#     set obj.http.Retry-After = "5";
#     synthetic {"

#  #  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

#   
#     
#   
#   
#     Error "} + obj.status + " " + obj.response + {"
#     

"} + obj.response + {"


#     

Guru Meditation:


#     

XID: "} + req.xid + {"


#     

#     

Varnish cache server


#   

# "};
#     return (deliver);
# }
#
 sub vcl_init {
        return (ok);
 }
#
 sub vcl_fini {
        return (ok);
 }

 

/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,1024m -T 127.0.0.1:200 -a 0.0.0.0:80

 

现在可以打开varnish了

http://192.168.88.1/    (开启varnish的端口号)

戴图显示,nginx 为177上运行的web服务器,出现了varnish字样,表示缓存架设成功,已正常工作了。


 

启动varnishncsa用来将Varnish访问日志写入日志文件:

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

配置开机自动启动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 &

 优化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服务器连接数与命中率
/usr/local/varnish/bin/varnishstat

 

配置启动脚本: 

mkdir /cache/varnish/V -p

vi /usr/local/varnish/sbin/start.sh

 

#!/bin/sh
#file:start.sh
date -u
/usr/local/varnish/sbin/varnishd -a 0.0.0.0:80  -s file,/cache/varnish/V,1024m  -f         /usr/local/varnish/etc/varnish/default.vcl  -p thread_pool_max=1500  -p thread_pools=5  -p listen_depth=512 

chmod 777 start.sh

运行:

/usr/local/varnish/sbin/start.sh


推荐阅读
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 如何配置Unturned服务器及其消息设置
    本文详细介绍了Unturned服务器的配置方法和消息设置技巧,帮助用户了解并优化服务器管理。同时,提供了关于云服务资源操作记录、远程登录设置以及文件传输的相关补充信息。 ... [详细]
  • 在Ubuntu 16.04 LTS上配置Qt Creator开发环境
    本文详细介绍了如何在Ubuntu 16.04 LTS系统中安装和配置Qt Creator,涵盖了从下载到安装的全过程,并提供了常见问题的解决方案。 ... [详细]
  • 使用Vultr云服务器和Namesilo域名搭建个人网站
    本文详细介绍了如何通过Vultr云服务器和Namesilo域名搭建一个功能齐全的个人网站,包括购买、配置服务器以及绑定域名的具体步骤。文章还提供了详细的命令行操作指南,帮助读者顺利完成建站过程。 ... [详细]
  • 本文详细介绍了VMware的多种认证选项,帮助你根据职业需求和个人技能选择最合适的认证路径,涵盖从基础到高级的不同层次认证。 ... [详细]
  • 本文介绍如何通过更改软件源来提前体验Ubuntu 8.10,包括详细的配置步骤和相关注意事项。 ... [详细]
  • 本文介绍如何通过SSH协议使用Xshell远程连接到Ubuntu系统。为了实现这一目标,需要确保Ubuntu系统已安装并配置好SSH服务器,并保证网络连通性。 ... [详细]
  • 在Ubuntu 8.04中安装美观的Cairo-Dock桌面增强工具
    本文介绍如何在Ubuntu 8.04系统中安装和配置Cairo-Dock,这款桌面增强工具以其精美的界面和高效的功能备受用户青睐。文章将指导您通过简单的步骤完成安装,并提供一些实用的配置建议。 ... [详细]
  • 云计算的优势与应用场景
    本文详细探讨了云计算为企业和个人带来的多种优势,包括成本节约、安全性提升、灵活性增强等。同时介绍了云计算的五大核心特点,并结合实际案例进行分析。 ... [详细]
  • 本文详细介绍了如何通过RPM包在Linux系统(如CentOS)上安装MySQL 5.6。涵盖了检查现有安装、下载和安装RPM包、配置MySQL以及设置远程访问和开机自启动等步骤。 ... [详细]
  • 阿里云ecs怎么配置php环境,阿里云ecs配置选择 ... [详细]
  • 本文详细介绍了如何在预装Ubuntu系统的笔记本电脑上安装Windows 7。针对没有光驱的情况,提供了通过USB安装的具体方法,并解决了分区、驱动器无法识别等问题。 ... [详细]
  • Nginx 反向代理与负载均衡实验
    本实验旨在通过配置 Nginx 实现反向代理和负载均衡,确保从北京本地代理服务器访问上海的 Web 服务器时,能够依次显示红、黄、绿三种颜色页面以验证负载均衡效果。 ... [详细]
  • 本文详细介绍了如何在PHP中进行数组删除、清空等操作,并提供了在Visual Studio Code中创建PHP文件的步骤。 ... [详细]
  • 本文探讨了为何相同的HTTP请求在两台不同操作系统(Windows与Ubuntu)的机器上会分别返回200 OK和429 Too Many Requests的状态码。我们将分析代码、环境差异及可能的影响因素。 ... [详细]
author-avatar
津pig
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有