如何在CentOS 7/RHEL 7上安装Varnish 4

时间:2019-08-20 17:58:17  来源:igfitidea点击:

Varnish是众所周知的前端网络缓存软件。而它也可以用作负载平衡器。

在/etc/yum.repos.d/中创建varnish.repo文件

vi /etc/yum.repos.d/varnish.repo

内容如下:

[varnish-4.0]
name=Varnish 4.0 for Enterprise Linux
baseurl=https://repo.varnish-cache.org/redhat/varnish-4.0/el7/$basearch
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-VARNISH

使用yum命令安装Varnish

yum install varnish varnish-libs varnish-libs-devel

查看varnish配置目录和文件。

Varnish配置目录:/etc/Varnish

Varnish配置文件:在/etc/varnish中创建了3个新文件

(a) default.vcl
(b) secret
(c) varnish.params

修改默认端口

默认情况下,Varnish 4监听端口6081。
其在文件varnish.params中定义。
将端口6081改为80,以便访问网站时,首先访问的是缓存在前端的Varnish。

vi /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80

设置Varnish缓存的存储方式

我们有两种选择:

  1. 保存在磁盘上的Varnish缓存
  2. 保存在内存上的Varnish缓存

在磁盘上设置Varnish缓存:

编辑Varnish.params,在文件上设置VARNISH和VARNISH参数。

vi /etc/varnish/varnish.params

下面给出的是VARNISH_存储的默认值。
存储文件varnish_storage.bin的大小为1GB

VARNISH_STORAGE="file,/var/lib/varnish/varnish_storage.bin,1G"

在RAM上设置Varnish缓存:

如果服务器的内存配置很高,有足够的空闲RAM用于Varnish, 那么使用这种方式缓存更快,效率更高。

vi /etc/varnish/varnish.params

我们指定2g内存作为Varnish存储。

VARNISH_STORAGE="malloc,2048m"

default.vcl文件

default.vcl是一套VCL规则,内置于Varnish。

# 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 = "127.0.0.1";
    .port = "8080";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you dont 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.
}

这个default.vcl文件不是每项都要配置。因此,“vcl_recv,vcl_backend_response”和“vcl_deliver”没有内容。

根据backend的定义,网络流量将连接127.0.0.1的8080端口进行http通讯。

backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

所以我们的Varnish和web服务在同一台服务器上运行。

现在根据配置,将web服务器监听端口改成8080。

将apache端口号改为8080

修改httpd.conf中Listen的值

vi /etc/httpd/conf/httpd.conf

Listen *:8080

更改Apache虚拟主机中的端口号:

vi /etc/httpd/conf/httpd.conf

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName  example.com
    DocumentRoot /var/www/html/
    ErrorLog logs/sample-error.log
    CustomLog logs/sampleaccess_log common
</VirtualHost>

启动/重启Varnish,Apache服务

重启Apache

在CentOS 7/RHEL 7服务器上重新启动apache服务。

systemctl restart httpd

Varnish服务器

systemctl restart varnishd

启用Varnish日志

日志对于任何服务都很重要,因此我们将启用Varnish日志记录。

重新启动varnishncsa和varnishlog服务。

重新启动varnishncsa

systemctl restart varnishncsa

##3 重新启动varnishlog

systemctl restart varnishlog

默认Varnish日志目录路径为/var/log/Varnish/