Varnsih调用多台后端主机
author:JevonWei
版权声明:原创作品
Varnsih调用多个后端主机
环境
Varnish 192.168.198.139
图片服务端 192.168.198.120
程序服务端 192.168.198.128
程序服务端(httpd+php)
[root@danran /]# yum -y install httpd php
[root@danran /]# vim /var/www/html/index.html
<h1> Test Page @BE Server
[root@danran /]# vim //var/www/html/index.php
<?php
phpinfo();
?>
[root@danran /]# systemctl start httpd
[root@danran /]# setenforce 0
[root@danran /]# iptables -F
[root@danran /]# ss -ntl
图片服务端(nginx,epel源)
[root@centos6 ~]# yum -y install nginx \\epel源
[root@centos6 ~]# vim /etc/nginx/conf.d/default.conf \\修改默认的网页根文件
server {
root /data/web/images;
}
[root@centos6 ~]# mkdir /data/web/images -pv
[root@centos6 ~]# find /usr/share/ -iname "*.jpg" -exec cp {} /data/web/images/ \; \\复制一些图片到/data/web/images目录下做测试用
[root@centos6 ~]# service nginx start
[root@centos6 ~]# iptables -F
vernish(epel源)
[root@danran ~]# yum -y install varnish
[root@danran varnish]# vim /etc/varnish/varnish.params
VARNISH_LISTEN_PORT=80 \\监听端口为80,默认为6081
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 \\监听管理接口的IP,默认为本机
VARNISH_ADMIN_LISTEN_PORT=6082 \\管理接口的端口,默认为6082
VARNISH_SECRET_FILE=/etc/varnish/secret \\认证密码文件
#DAEMON_OPTS="-p thread_pool_min=5 -p thread_pool_max=500 -p thread_pool_timeout=300" \\定义运行时参数
[root@danran varnish]# vim /etc/varnish/default.vcl
backend appsrv1 { \\定义appsrv1用来存放网页文件
.host = "192.168.198.128";
.port = "80";
}
backend imgsrv1 { \\定义imgsrv1用来存放图片等静态文件
.host = "192.168.198.120";
.port = "80";
}
sub vcl_recv {
if (req.url ~ "(?i)\.(jpg|jpeg|png|gif|svg)$") {
set req.backend_hint = imgsrv1;
} else {
set req.backend_hint = appsrv1;
}
}
[root@danran varnish]# systemctl start varnish
client
[root@danran ~]# curl 192.168.198.139/index.html
<h1> Test Page @BE Server \\数据来自程序服务端
[root@danran ~]# curl 192.168.198.139/cloud.jpg 数据来自图片服务端
Varnish调用后端服务器组
Director配置后端服务组
将图片保存在后端的图片服务器组中,多台图片服务器组成图片服务器组
受条件限制,在此创建一个虚拟主机做物理主机使用,虚拟主机使用8080端口,从而使虚拟主机的8080端口和原有的80端口组成我们需要的后端图片服务器组
图片服务端创建虚拟主机
[root@centos6 ~]# vim /etc/nginx/conf.d/vhost2.conf
server {
listen 8080;
server_name img1.danran.com;
root "/data/web/image2";
}
[root@centos6 ~]# vim /data/web/image2/test.txt
Image Server 2
[root@centos6 ~]# nginx -t
[root@centos6 ~]# nginx -s reload
80和8080两个端口作为两个物理主机使用,从而构建服务器组
[root@centos6 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::52622 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:8080 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 *:80 *:*
Vernish
[root@danran ~]# vim /etc/varnish/default.vcl
导入directors模块
import directors;
定义一个app程序后端服务器
backend appsrv1 {
.host = "192.168.198.128";
.port = "80";
}
定义两个图片服务端
backend imgsrv1 {
.host = "192.168.198.120";
.port = "80";
}
backend imgsrv2 {
.host = "192.168.198.120";
.port = "8080";
}
定义一个图片服务器组imgsrvs,并将imgsrv1和imgsrv2两个后端图片服务器添加进imgsrvs组中
sub vcl_init {
new imgsrvs = directors.round_robin(); \\指定调度算法为轮询
imgsrvs.add_backend(imgsrv1);
imgsrvs.add_backend(imgsrv2);
}
sub vcl_recv {
if (req.url ~ "(?i)\.(jpg|jpeg|png|gif|svg|txt)$") {
set req.backend_hint = imgsrvs.backend();
} else {
set req.backend_hint = appsrv1;
}
}
[root@danran ~]# varnish_reload_vcl \\重新加载/etc/varnish/default.vcl参数文件
client(imgsrv1和imgsrv2轮询调度)
[root@danran ~]# curl 192.168.198.139/test.txt
Image Server 1
[root@danran ~]# curl -I 192.168.198.139/test.txt
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Tue, 23 May 2017 04:16:31 GMT
Content-Type: text/plain
Content-Length: 15
Last-Modified: Tue, 23 May 2017 04:11:20 GMT
ETag: "5923b668-f"
X-Varnish: 32831 67
Age: 37
Via: 1.1 varnish-v4
X-Cache: Hit via 192.168.198.139
Connection: keep-alive
Vernish清除缓存
[root@danran ~]# curl -X PURGE 192.168.198.139/test.txt
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Error 200 Purged</h1>
<p>Purged</p>
<h3>Guru Meditation:</h3>
<p>XID: 71</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
[root@danran ~]# curl 192.168.198.139/test.txt
Image Server 2
基于cookie的session sticky
Vernish
[root@danran ~]# vim /etc/varnish/default.vcl
导入directors模块
import directors;
定义一个app程序后端服务器
backend appsrv1 {
.host = "192.168.198.128";
.port = "80";
}
定义两个图片服务端
backend imgsrv1 {
.host = "192.168.198.120";
.port = "80";
}
backend imgsrv2 {
.host = "192.168.198.120";
.port = "8080";
}
sub vcl_init {
new h = directors.hash();
h.add_backend(imgsrv1, 1); // backend 'imgsrv1' with weight '1'
h.add_backend(imgsrv2, 1); // backend 'imgsrv2' with weight '1'
}
sub vcl_recv {
// pick a backend based on the cookie header of the client
set req.backend_hint = h.backend(req.http.cookie);
}
danran
分类:
Linux实战
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库