Varnsih调用多台后端主机
生活随笔
收集整理的這篇文章主要介紹了
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);
}
總結
以上是生活随笔為你收集整理的Varnsih调用多台后端主机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 气动带真空大米颗粒自动抽气包装机设备
- 下一篇: 一体化气象站:远程监控和数据实时传输的气