• varnish安装与配置


    1、安装

    文件总数为5个,包含2个安装文件,1个安装脚本,2个配置文件。

    上传至静态缓存服务器 /home/{user}/ Varnish目录下(使用root用户,否则安装过程中权限不够)

    pcre-8.35.tar.gz           #Varnish依赖库
    
    varnish-3.0.5.tar.gz       #Varnish安装文件
    
    varnish_init.sh         #Varnish安装脚本
    
    varnish                 #Varnish启动配置文件
    
    default.vcl         #Varnish主配置文件

    依赖库安装

    Varnish用到了pcre这个库。在操作系统安装过程中可以找到pcre的安装,可以在安装操作系统时候默认安装这个库。如果没有安装pcre,在编译varnish2.0以上版本时,会提示找不到pcre库,而pcre库是为了兼容正则表达式,所以必须要安装pcre库。

    # tar --zxvf pcre-8.35.tar.gz
    
    # cd pcre-8.35
    
    # ./configure
    
    # make && make install
    
    # mkdir /usr/local/lib/.libs
    
    # cp /usr/local/lib/libpcre.a  /usr/local/lib/.libs/libpcre.a
    
    # cp /usr/local/lib/libpcre.la  /usr/local/lib/.libs/libpcre.la

    Varnish安装

    使用自定义的一键安装脚本(redhat版):varnish_init.sh。其中包含了安装软件,设置service及自启动。使用命令为:

    #sh  varnish_init.sh 安装包绝对路径

    如下:

    #cd  /home/huangteng/Varnish  #先切换到varnish_init.sh所在的目录
    
    #sh  varnish_init.sh  /home/huangteng/Varnish/varnish-3.0.5.tar.gz

    出现以下提示即为安装成功:

    varnish install success.  #安装成功
    
    varnish copy file success. #service及自启动设置成功

    2、配置文件

    拷贝配置文件至以下目录

    #cp varnish /etc/sysconfig/varnish   #复制启动文件(选yes覆盖)
    
    #cp default.vcl /etc/varnish/default.vcl #复制主复制文件(选yes覆盖)

    将默认的两个文件分别覆盖掉

    在安装过程中已经将vainish加入系统服务中,通过chkconfig命令可以设置开机自动启动

    #chkconfig varnish on

    通过:

    #service varnish start|stop|restart

    命令管理varnish服务

    varnish为Varnish的启动配置文件。全路径为:“/etc/sysconfig/varnish”,下面列出了varnishd常用参数的使用方法和含义。

        -a, address:port           :表示varnish对httpd的监听地址及其端口  
        -b, address:port           :表示后端服务器地址及其端口  
        -d, debuf                 :表示使用debug调试模式  
        -f, file                   :指定varnish服务器的配置文件  
        -p, param=value          :指定服务器参数,用来优化varnish性能  
        -P,  file                   :Varnish进程PID文件存放路径  
        -n, dir                    :指定varnish的工作目录  
    -s, kind[,options]    :指定varnish缓存内容的存放方式,
                        常用的方式有:“-s file,<dir_or_file>,<size>”。         其中“<dir_or_file>”指定缓存文件的存放路径,“<size>”指定缓存文件的大小 -t, time to live   :指定缺省的TTL值 -T, address:port   :设定varnish的telnet管理地址及其端口 -w, int[,int[,int]] :设定varnish的工作线程数,常用的方式有: -w min,max -w min,max,timeout 例如:-w 5,51200,30,这里需要说明下,在varnish2.0版本以后,最小启动的线程数不能设定过大,设置过大,会导致varnish运行异常缓慢。 -V 显示varnish版本号和版权信息

    default.vcl为varnish的缓冲配置:

    配置后端服务器:

    backend img {
    
    .host = "192.168.136.131";
    
    .port = "8080";
    
    }

     配置

    sub vcl_fetch
    {
    
        # 设定默认缓存时长
    
        set beresp.ttl = 300s; #超时时长为300秒
    
        # 定义缓存时长
    
        if (req.url ~ ".(css|js|html|htm|xsl|xml)$") {
    
            set beresp.ttl = 300s; #超时时长为300秒
    
        }
    
    
        # 图片/视频/音频/文档/压缩文件 缓存时常 三天 3x24x60x60m
    
        if (req.url ~ ".(png|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg)$") {
    
            set beresp.http.Pragma = "cache";
    
            set beresp.http.Cache-Control = "max-age=259200, public";
    
            set beresp.ttl = 3d;
    
        }
    
    
        # 如果后端状态为 404 则缓存 600s
    
        if(beresp.status == 400 && beresp.status == 403 && beresp.status == 404) {
    
            set beresp.ttl = 600s;
    
        }
     
    
        # 如果后端服务器出现 500/501/502/503/504 错误时进入神圣模式 saintmode
    
        if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {
    
            set beresp.saintmode = 20s;
    
            return (restart);
    
        }
    
        # grace时长,减少高并发下的穿透和争抢,设置缓存对象过期后还可以保留的时间
    
        set beresp.grace = 5m;
    
        return (deliver);
    
    }

    测试

    配置tomcat服务器放置图片然后通过访问varnish->tomcat访问图片

    curl –I http://192.168.136.131:8081/hhh/img/b999.jpg

    命令可以查看到

     

    X-cache:命中

  • 相关阅读:
    Selenium的使用
    Redis防护建议
    爬虫文件存储-3:Redis
    爬虫文件存储-2:MongoDB
    爬虫文件存储-1:mysql
    爬虫文件存储:txt文档,json文件,csv文件
    Python MongoDB 教程
    使用Robo 3T 软件管理MongoDB数据库如何执行命令行shell
    使用Scrapy爬取图书网站信息
    解决Scrapy抓取中文网页保存为json文件时中文不显示而是显示unicode的问题
  • 原文地址:https://www.cnblogs.com/huangt/p/4597613.html
Copyright © 2020-2023  润新知