Varnish开源HTTP反向代理缓存服务器、部署安装、测试
·
一、概述
Varnish是一个高性能的开源HTTP反向代理缓存服务器,它可以加速动态内容的交付并降低服务器的负载。Varnish常被用作HTTP加速器和负载均衡器,通过缓存静态文件、动态内容甚至整个页面来减少请求的数量和服务器响应时间。它支持各种缓存规则,可以针对不同的用户或请求类型缓存不同的信息,并提供丰富的管理工具和API以便于配置和监控。Varnish在Web性能优化和高可用性方面发挥着重要作用,被广泛应用于许多高流量的网站和Web应用程序中。
二、安装部署
[root@localhost ~] curl -L -o /etc/yum.repos.d/varnish.repo https://packagecloud.io/varnishcache/varnish41/el/7/$basearch.repo #添加Varnish的yum源
[root@localhost ~] yum -y install varnish
配置文件
Varnish的配置文件位于/etc/varnish/default.vcl。如前所述,你可以根据需要在其中定义缓存规则、后端响应规则和传输规则。以下是一个示例:
backend default {
.host = "127.0.0.1"; #改成自己IP
.port = "8080";
}
sub vcl_recv {
# Define cache rules here
}
sub vcl_backend_response {
# Define backend response rules here
}
sub vcl_deliver {
# Define delivery rules here
}
这个示例配置了Varnish使用本地主机的8080端口作为后端,并留出了三个子例程用于定义缓存规则、后端响应规则和传输规则。你可以根据需要更改这些规则。
三、测试
配置测试站点
[root@localhost ~] systemctl start varnish
[root@localhost ~] yum -y install httpd
[root@localhost ~] vim /etc/httpd/conf.d/varnish-test.conf
Listen 8080
<VirtualHost *:8080>
DocumentRoot /var/www/html/varnish-test
ErrorLog logs/varnish-test-error_log
CustomLog logs/varnish-test-access_log combined
</VirtualHost>
[root@localhost ~] mkdir /var/www/html/varnish-test
[root@localhost ~] cd /var/www/html/varnish-test
[root@localhost ~] vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Varnish Test</title>
</head>
<body>
<h1>Hello, Varnish!</h1>
</body>
</html>
测试Varnish
[root@localhost ~] echo "CacheDisable on" | sudo tee /var/www/html/varnish-test/.htaccess
[root@localhost ~] systemctl start httpd
[root@localhost ~] curl -I http://192.168.1.122:6081/
HTTP/1.1 403 Forbidden
Date: Tue, 13 Jun 2023 11:16:13 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
X-Varnish: 5
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive
[root@localhost ~] curl -I http://192.168.1.122:6081
HTTP/1.1 200 OK
Date: Tue, 13 Jun 2023 12:14:02 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Tue, 13 Jun 2023 12:11:19 GMT
ETag: "7b-5fe01bb97b9f8"
Content-Length: 123
Content-Type: text/html; charset=UTF-8
X-Varnish: 32778 3
Age: 5 #同一个会叠加次数
Via: 1.1 varnish-v4
Connection: keep-alive
更多推荐
已为社区贡献2条内容
所有评论(0)