varnish实战

作者: 4e8ea348373f | 来源:发表于2018-04-19 11:38 被阅读5次

    环境

      1.两台contos7 的虚拟机分别为node1,node2
      2.关闭防火和selinux

    安装

    #varnish 在epel
    [root@node1 yum.repos.d]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    [root@node1 yum.repos.d]# rpm -ivh epel-release-latest-7.noarch.rpm
    [root@node1 ~]# yum install varnish -y
    

    varnish如何存储缓存对象

      file:单个文件,不支持持久机制
      malloc:内存
      persistent:基于文件的持久存储

    配置varnish的三种应用

      1.varnishd应用程序的命令行参数(主程序)
        监听的socket,使用的存储类型等等,额外的配置参数
          -p param=value
          -r param,param,... 设置只读参数列表
          /etc/varnish/varnish.params
        -p选项指明的参数
      2.运行时参数
          可在程序运行中,通过cli接口进行配置
      3.vcl 配置缓存系统缓存机制
        通过vcl配置文件进行配置 ,先编译,后应用,依赖gcc

    编辑配置文件

    [root@node1 varnish]# vim varnish.params  (配置主程序)
    # Varnish environment configuration description. This was derived from
    # the old style sysconfig/defaults settings
    
    # Set this to 1 to make systemd reload try to switch VCL without restart.
    RELOAD_VCL=1
    
    # Main configuration file. You probably want to change it.
    VARNISH_VCL_CONF=/etc/varnish/default.vcl
    
    # Default address and port to bind to. Blank address means all IPv4
    # and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
    # quad, or an IPv6 address in brackets.
    # VARNISH_LISTEN_ADDRESS=192.168.1.5
    VARNISH_LISTEN_PORT=6081
    
    # Varnish environment configuration description. This was derived from
    # the old style sysconfig/defaults settings
    
    # Set this to 1 to make systemd reload try to switch VCL without restart.
    RELOAD_VCL=1
    
    # Main configuration file. You probably want to change it.
    VARNISH_VCL_CONF=/etc/varnish/default.vcl
    
    # Default address and port to bind to. Blank address means all IPv4
    # and IPv6 interfaces, otherwise specify a host name, an IPv4 dotted
    # quad, or an IPv6 address in brackets.
    # VARNISH_LISTEN_ADDRESS=192.168.1.5
    VARNISH_LISTEN_PORT=6081
    
    # Admin interface listen address and port
    VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
    VARNISH_ADMIN_LISTEN_PORT=6082
    
    # Shared secret file for admin interface
    VARNISH_SECRET_FILE=/etc/varnish/secret  #连接配置接口,用的域认证文件
    
    # Backend storage specification, see Storage Types in the varnishd(5)
    # man page for details.
    VARNISH_STORAGE="malloc,256M"   这里是内存方式,可以改为文件
    
    # User and group for the varnishd worker processes
    VARNISH_USER=varnish
    VARNISH_GROUP=varnish
    
    # Other options, see the man page varnishd(1)
    DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300"    #开启线程池,管理最多可以启动的线程数量
    
    .............................................................................
    #配置vcl,缓存
    [root@node1 varnish]# vim default.vcl 
    #
    # This is an example VCL file for Varnish.
    #
    # It does not do anything by default, delegating control to the
    # builtin VCL. The builtin VCL is called when there is no explicit
    # return statement.
    #
    # See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
    # and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
    
    # Marker to tell the VCL compiler that this VCL has been adapted to the
    # new 4.0 format.
    vcl 4.0;
    
    # Default backend definition. Set this to point to your content server.
    backend default {
        .host = "192.168.66.130";    #配置后端服务器
        .port = "80";
    }
    
    sub vcl_recv {
        # Happens before we check if we have this in cache already.
        #
        # Typically you clean up the request here, removing cookies you don't need,
        # rewriting the request, etc.
    }
    
    sub vcl_backend_response {
        # Happens after we have read the response headers from the backend.
        #
        # Here you clean the response headers, removing silly Set-Cookie headers
        # and other mistakes your backend does.
    }
    
    sub vcl_deliver {
        # Happens when we have all the pieces we need, and are about to send the
        # response to the client.
        #
        # You can do accounting or modifying the final object here.
    }
    
    

    配置node2

      node2 作为后端主机 ,这里使用httpd
      安装配置这里不说了
      生成测试文件并启动httpd

    root@node2 ~]# for i in {1..10}; do echo "page $i on web1" > /var/www/html/test$i.html; done
    [root@node2 ~]# ll /var/www/
    cgi-bin/ html/    
    [root@node2 ~]# ll /var/www/html/
    总用量 44
    -rw-r--r-- 1 root root 15 4月  18 15:17 index.html
    -rw-r--r-- 1 root root 16 4月  19 11:12 test10.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test1.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test2.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test3.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test4.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test5.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test6.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test7.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test8.html
    -rw-r--r-- 1 root root 15 4月  19 11:12 test9.html
    [root@node2 ~]# systemctl start httpd
    

    在node1启动varnish服务

    [root@node1 varnish]# systemctl start varnish
    

    测试

      访问http://192.168.66.129:6081/test1.html 6081可以在主程序配置文件中修改为80

    管理接口

    #使用varnishadm -S 指明认证文件, 如果要远程连接,需要修改主程序的监听地址为公网地址
    [root@node1 varnish]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082   guanl 
    

    日志接口

    共享内存日志工具
    [root@node1 varnish]# varnishlog      #可以用systemctl start varnishlog 启动周期性的日志服务
    [root@node1 varnish]# varnishncsa   #可以用systemctl start varnishncsa 启动周期性的日志服务
    [root@node1 varnish]# varnishtop
    [root@node1 varnish]# varnishstat
    

    至此varnish已经完成,至于vcl的用法和解释,请参考下节https://www.jianshu.com/writer#/notebooks/24512432/notes/26857644

    相关文章

      网友评论

        本文标题:varnish实战

        本文链接:https://www.haomeiwen.com/subject/fxoikftx.html