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

相关推荐

  • 运维工程师技能需求排行

    这是我今天在拉勾网搜索运维,翻完了4四页也招聘信息之后得到的,我的目的是想要看看之后的学习,哪个更应该成为重点,有些在我意料之中,有些还真的没想到,算是努力了一个小时的收获吧,分享给大家。
    注意:其中的看法仅代表个人观点,很多都是依靠我自己的学习经验和工作经验累积的

    Linux干货 2017-12-12
  • Linux Shell(脚本)编程入门

    为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活的工具。Shell不仅仅是命令的收集,而且是一门非常棒的编程语言。您可以通过使用shell使大量的任务自动化,shell特别擅长系统管理任务,尤其适合那些易用性、可维护性和便携性比效率更重要的任务。 下面,让我们一起来看看shell是如何工作的: 建…

    Linux干货 2017-08-05
  • Ansible的常用模块

    command模块: 目的:在指定节点上运行hostname命令 命令:ansible 192.168.1.16 -m command  ‘hostname’copy模块:目的:把主控端/root目录下的a.sh文件拷贝到到指定节点上 命令:ansible 192.168.1.16 -m copy -a ‘src=/roo…

    Linux干货 2017-10-31
  • 马哥教育网络班20期+第6周练习博客

    请详细总结vim编辑器的使用并完成以下练习题 1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#; [root@bogon ~]# cp /etc/rc.d/rc.sysinit  /tmp/rc.sysinit […

    Linux干货 2016-07-22
  • OpenSSH

    ssh: secure shell, protocol, 22/tcp, 安全的远程登录 OpenSSH: ssh协议的开源实现; dropbear:另一个开源实现;  SSH协议版本 v1: 基于CRC-32做MAC,不安全;man-in-middle v2:双方主机协议选择安全的MAC方式 基于DH算法做密钥交换,基于RSA或DSA算法实现身份…

    Linux干货 2015-06-21
  • bash的工作特性-命令执行状态返回值、命令行展开

    bash的基础特性:命令的执行状态结果 命令执行的状态结果:      bash通过状态返回值来输出此结果:           成功:0           失败:1-255 命令执行完成之后,其状态返回值会保存于bash的特殊…

    Linux干货 2016-08-22