nginx+varnish+httpd小试牛刀

简介: Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点;

此次试验的目的是让Nginx做前端访问负载均衡,varnish代理后端的web服务器,并缓存结果

1.准备机器,做好时间同步,主机名解析

192.168.42.150 node1 [nginx 负载均衡]
192.168.42.151 node2 [varnish1]
192.168.42.152 node3 [varnish2]
192.168.42.153 node4 [web1 httpd]
192.168.42.154 node5 [web2 httpd]

2.这次我们也是从后面的节点做起吧

node5:

(1).安装httpd

yum install httpd -y

(2).创建网站目录

mkdir /application/test -pv
chown  -R apache.apache /application/test
cd /application/test
echo "this is test 2 page." >index.html

(3).配置虚拟主机

cd /etc/httpd/conf.d
vim test.conf
<VirtualHost *:80>
        ServerName test.varnish.com
        DocumentRoot "/application/test"
        <Directory "/application/test">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/test.varnish.com_access_log" combined
</VirtualHost>

(4).启动httpd,并测试一下子

systemctl start httpd
[root@node5 /]# curl 127.0.0.1
this is test 2 page.

node4同上,只需修改 /application/test/index.html

echo "this is test 1 page." >index.html

启动并测试

3.node3,node2 安装varnish

node3:

(1).安装varnish

yum install varnish -y

(2).varnish服务端配置

cd /etc/varnish/
cp default.vcl  default.vcl.back
[root@node3 varnish]# ls
default.vcl  default.vcl.back  secret  varnish.params
[root@node3 varnish]# vim varnish.params 

VARNISH_LISTEN_PORT=80
#VARNISH_STORAGE="malloc,256M"
VARNISH_STORAGE="file,/data/varnish/cache,1G"

(3).创建缓存目录

mkdir /data/varnish/cache -pv
chown -R varnish.varnish  /data/varnish/cache

(4).启动varnish

systemctl start varnish
[root@node3 varnish]# ss -tnl
State      Recv-Q Send-Q    Local Address:Port      Peer Address:Port              
LISTEN     0      1024                  *:80                   *:*                  
LISTEN     0      128                   *:22                   *:*                  
LISTEN     0      100           127.0.0.1:25                   *:*                  
LISTEN     0      10            127.0.0.1:6082                 *:*                  
LISTEN     0      1024                 :::80                  :::*                  
LISTEN     0      128                  :::22                  :::*                  
LISTEN     0      100                 ::1:25                  :::*

此时我们去浏览器访问是出错的,因此我们需要配置default.vcl,指向后端的web

Error 503 Backend fetch failed
Backend fetch failed
Guru Meditation:
XID: 32796
Varnish cache server

(5).配置default.vcl,先配置一台测试一把

vim default.vcl
backend default {
    .host = "192.168.42.153";
    .port = "80";
}

(6).重新加载配置问件

varnish_reload_vcl

Loading vcl from /etc/varnish/default.vcl
Current running config name is 
Using new config name reload_2017-06-27T09:15:28
VCL compiled.
VCL 'reload_2017-06-27T09:15:28' now active
available       0 boot
active          0 reload_2017-06-27T09:15:28

Done

(7).去浏览器刷新一把,ok
(8).因为我们的后端web是有多台的,因此我们需要把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;
import directors;

#健康状态检查公共变量
probe www_probe {
   .url = "/index.html";
   .interval = 1s;
   .timeout = 1s;
   .window = 8;
   .threshold = 5;
}

#web1
backend websrv1 {
    .host = "192.168.42.153";
    .port = "80";
    .probe = www_probe;
}
#web2
backend websrv2 {
    .host = "192.168.42.154";
    .port = "80";
    .probe = www_probe;
}

#初始化
sub vcl_init {
     new websrvs= directors.round_robin();
     websrvs.add_backend(websrv1);
     websrvs.add_backend(websrv2);
}

#允许清除缓存的ip
acl purgers{
    "127.0.0.1";
    "192.168.42.0"/24;    
}

sub vcl_recv {

    #调用轮询
    set req.backend_hint = websrvs.backend();    

    #对某类资源的请求不检查缓存 
    if (req.url ~ "(?i)^/(login|admin)") {
        return(pass);
    }
    #将客户端的主机推送给后端的web,写入后端web日志中
    if (req.restarts == 0) {
        if (req.http.X-Fowarded-For) {
            set req.http.X-Forwarded-For = req.http.X-Forwarded-For + "," + client.ip;
            } else {
            set req.http.X-Forwarded-For = client.ip;
        }
    }
    #如果是PURGE就清除缓存
    if (req.method == "PURGE") {
        if (!client.ip ~ purgers) {
            return(synth(405,"Purging not allowed for " + client.ip));
        }
        return(purge);
    }
}

#对于特定类型的资源,例如公开的图片等,取消其私有标识,
#并强行设定其可以由varnish缓存的时长
sub vcl_backend_response {
    if (beresp.http.cache-control !~ "s-maxage") {
        if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js)$") {
            unset beresp.http.Set-Cookie;
            set beresp.ttl = 3600s;
        }
    }
}

#自定义显示缓存是否命中
sub vcl_deliver {
    if (obj.hits>0) {
        set resp.http.X-Cache = "HIT via " + server.ip;
    } else {
        set resp.http.X-Cache = "MISS via " + server.ip;
    }
}

(10).配置完以后,把配置文件推送到node2节点上

scp default.vcl varnish.params  192.168.42.151:/etc/varnish/

在node2节点上操作:创建缓存目录

mkdir /data/varnish/cache -pv
chown -R varnish.varnish  /data/varnish/cache

启动varnish并查看一下子

[root@node2 varnish]# systemctl start varnish
[root@node2 varnish]# ss -tnl
State      Recv-Q Send-Q   Local Address:Port     Peer Address:Port              
LISTEN     0      1024                 *:80                  *:*                  
LISTEN     0      128                  *:22                  *:*                  
LISTEN     0      100          127.0.0.1:25                  *:*                  
LISTEN     0      10           127.0.0.1:6082                *:*                  
LISTEN     0      1024                :::80                 :::*                  
LISTEN     0      128                 :::22                 :::*                  
LISTEN     0      100                ::1:25                 :::*

(11).在node4,node5上多添加几个虚拟主机

虚拟主机配置如下:

[root@node4 test1]# cat /etc/httpd/conf.d/test.conf 
<VirtualHost *:80>
        ServerName test.varlish.com
        DocumentRoot "/application/test"
        <Directory "/application/test">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access_log" combined
</VirtualHost>

<VirtualHost *:80>
        ServerName test.varlish1.com
        DocumentRoot "/application/test1"
        <Directory "/application/test1">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access1_log" combined
</VirtualHost>

<VirtualHost *:80>
        ServerName test.varlish2.com
        DocumentRoot "/application/test2"
        <Directory "/application/test2">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access2_log" combined
</VirtualHost>
<VirtualHost *:80>
        ServerName test.varlish3.com
        DocumentRoot "/application/test3"
        <Directory "/application/test3">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access3_log" combined
</VirtualHost>
<VirtualHost *:80>
        ServerName test.varlish4.com
        DocumentRoot "/application/test4"
        <Directory "/application/test4">
                Options None
                AllowOverride None
                Require all granted
        </Directory>
        CustomLog "logs/iounix_access4_log" combined
</VirtualHost>

网站目录如下:

[root@node4 application]# ll /application/
total 0
drwxr-xr-x 2 apache apache 24 Jun 27 10:03 test
drwxr-xr-x 2 apache apache 24 Jun 27 15:21 test1
drwxr-xr-x 2 apache apache 24 Jun 27 15:17 test2
drwxr-xr-x 2 apache apache 24 Jun 27 15:17 test3
drwxr-xr-x 2 apache apache 24 Jun 27 15:18 test4

文件结构如下:

[root@node4 application]# tree /application/
/application/
├── test
│   └── index.html
├── test1
│   └── index.html
├── test2
│   └── index.html
├── test3
│   └── index.html
└── test4
    └── index.html

每个index.html的内容都需要不一样哦

hosts文件域名解析如下:

192.168.42.153 test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

访问一下子:

curl  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

this is test 1 page.
test1 page
test2 page
test3 page
test4 page

以上是我在node4上的操作,而node5只需要把node4的配置文件和网站目录推送过去稍微改一下即可

scp /etc/hosts 192.168.42.154:/etc/hosts
cd /application
scp  -rp  test1 test2 test3 test4 192.168.42.154:/application/ 
scp /etc/httpd/conf.d/test.conf  192.168.42.154:/etc/httpd/conf.d/

然后去node5操作:

chown -R  apache.apache /application/*
echo "node5  11111111111111111" > /application/test1/index.html
echo "node5  22222222222222222" > /application/test2/index.html
echo "node5  33333333333333333" > /application/test3/index.html
echo "node5  44444444444444444" > /application/test4/index.html

vim /etc/hosts
192.168.42.154 test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
systemctl restart httpd

[root@node5 application]# curl test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
this is test 2 page.
node5  11111111111111111
node5  22222222222222222
node5  33333333333333333
node5  44444444444444444

现在我们在node4,node5上都弄了5个虚拟主机,并且访问正常,接下来就是让varnish能够正常访问那个5个虚拟主机,其实很简单,直接在varnish机器上 hosts文件添加对应的ip

node3:

vim /etc/hosts
192.168.42.152  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

测试一把: (1).每次访问需要清除缓存

curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

(2).访问

curl test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

ok,可以负载均衡

node2:

vim /etc/hosts
192.168.42.151  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

测试一把: (1).每次访问需要清除缓存

curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

(2).访问

curl test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

ok,也是可以负载均衡

4.node1安装nginx 负载均衡

(1).安装nginx

yum install  nginx -y

(2).配置nginx 因为我们的后端做了5个域名的虚拟主机,因此我们的负载均衡器也需要做5个一样的虚拟主机

hosts解析:

vim /etc/hosts
192.168.42.150  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

upsteam配置:

vim /etc/nginx/nginx.conf
upstream testsrvs {
    server 192.168.42.151:80;
    server 192.168.42.152:80;
    least_conn;
}

虚拟主机配置:

vim /etc/nginx/conf.d/test.conf

server {
  listen 80;
  server_name test.varlish.com;
  location / {
        proxy_pass http://testsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

server {
  listen 80;
  server_name test.varlish1.com;
  location / {
        proxy_pass http://testsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

server {
  listen 80;
  server_name test.varlish2.com;
  location / {
        proxy_pass http://testsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

server {
  listen 80;
  server_name test.varlish3.com;
  location / {
        proxy_pass http://testsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

server {
  listen 80;
  server_name test.varlish4.com;
  location / {
        proxy_pass http://testsrvs;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        add_header X-Via  $server_addr;
        add_header X-Accel $server_name;
  }
}

测试一把: 为了能够测出负载均衡的效果,我们先去node2,node3的两个varnish两个节点清除缓存
(1).清除缓存

curl -X PURGE test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com

然后在node1中访问:

第一次:

[root@node1 nginx]# curl  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
this is test 1 page.
test1 page
node5  22222222222222222
node5  33333333333333333
test4 page

第二次:

[root@node1 nginx]# curl  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
this is test 1 page.
node5  11111111111111111
node5  22222222222222222
test3 page
test4 page

第三次:

[root@node1 nginx]# curl  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
this is test 2 page.
node5  11111111111111111
test2 page
test3 page
node5  44444444444444444

第四次:

[root@node1 nginx]# curl  test.varlish.com test.varlish1.com test.varlish2.com  test.varlish3.com  test.varlish4.com
this is test 2 page.
test1 page
test2 page
node5  33333333333333333
node5  44444444444444444

好了,我们经过四次的访问得到不同的结果,说明负载均衡OK了

5.压测 ab -n 10000 -c 1000 http://test.varlish.com/

原创文章,作者:srayban,如若转载,请注明出处:http://www.178linux.com/78690

(0)
sraybansrayban
上一篇 2017-06-27
下一篇 2017-06-27

相关推荐

  • 安装CentOS6.8操作系统

        工具:     VMware Workstation 12     http://www.vmware.com/cn/products/workstation/workstation-evaluation.html  &n…

    Linux干货 2016-08-04
  • ELK-5.5.1部署

    ELK简介 ELK Stack是软件集合Elasticsearch、Logstash、Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统。 其中,Elasticsearch 是一个基于 Lucene 的、支持全文索引的分布式存储和索引引擎,主要负责将日志索引并存储起来,方便业务方检索查询。 Logstash是一个日志收集、过滤、转发…

    2017-12-04
  • N26第二周博客作业

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 Ø  ls 查看文件与目录 ls [选项] 文件或目录 常用选项 -a:全部的文件,连同隐藏文件(以.开头)一起列出来 -d:列出目录本身,而不是列出目录里的文件 -l:列出详细信息,如文件的属性与权限等数据 -h:将文件容量以易读的方式显示(例如:GB,KB等) -t:…

    Linux干货 2017-01-08
  • 第二十六天 iptables 初识

      Iptables是位于用户空间,是linux系统上的防火墙管理配置规则的工具,主要用于添加、删除、管理netfilter的规则,   Netfilter是位于内核中真正的防火墙,由5个钩子组成,也叫五个规则链。 Netfilter的作用:起到过滤封包,转换与映射IP地址和端口,拆分和修改封包内容,追踪封包等功能 Iptabl…

    Linux干货 2016-07-02
  • DNS-BIND

    1.实验环境 服务器类型 域名 IP 主DNS服务器 test.com. 192.5.24.101 从DNS服务器 192.5.24.102 子DNS服务器 ops.test.com. 192.5.24.201 备注:所有服务器需保持时间同步。 2.正向解析区域(192.5.24.101) 1)  安装bind 2)  编辑主配置文件,修…

    2017-05-31
  • grep与正则表达式

    1.什么是grep grep(Golobal Regular Expression print)是Linux系统中一个强大的文本搜索工具,也是俗称的搜索三兄弟之一,grep的最大意义就是搜索文本,把匹配的行打印到屏幕上,但不影响原文件的内容;在搜索文本的过程中,可以利用到“正则表达式”来定以自己的搜索匹配模式。 Unix的grep家族包括了grep、egre…

    Linux干货 2015-10-08