小fish 发表于 2018-11-8 10:40:48

nginx varnish

  1.前端nginx做调度器及反代服务器,将用户的请求调度至后端的两台varnish,缓存调度算法使用一致性hash算法保证缓存命中率;
  2.两台varnish反向代理用户请求至三个(组)后端主机,分别为存储静态资源(htm,html,css,js),应用程序服务器(可以部署wordpress或Discuz!),图片统一保存至图片服务器;
  3.用户登录后,可以通过wordpress发布新的博文,并且可以上传图片;
  4.如果后端主机全部宕机,varnish可以使用过期缓存响应客户端;
  实验规划:
  director1:    172.16.1.4 nginx+keepalived 主
  director2:    172.16.1.2 nginx+keepalived 备
  varnish1:   172.16.1.5
  varnish2:   172.16.1.6
  static server:172.16.1.10
  php server:   172.16.1.3
  拓扑图
  blob.png
  一、配置两台nginx调度器主机
  director1的keepalived配置
  11111.PNG
  director2的keepalived配置
  22222.PNG
  两台nginx的配置
  在nginx.conf配置文件中的http段内添加upstream内容,将后端两台varnish服务器加入到该upstream中,同时做一致性hash算法保证缓存命中率。
  后端健康状态检查设置:max_fails=1设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不可用。在下一个fail_timeout时间段,服务器不会再被尝试。
  33333.PNG
  二、两台varnish主机的配置
  更改varnish的监听端口为80
  # vim /etc/varnish/varnish.params
  VARNISH_LISTEN_PORT=80
  varnish配置文件内容
  # vim /etc/varnish/default.vcl
  4444.PNG
  5555.PNG
  编译使配置生效
  # varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
  200
  Varnish Cache CLI 1.0
  Linux,3.10.0-327.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
  varnish-4.0.5 revision 07eff4c29
  Type 'help' for command list.
  Type 'quit' to close CLI session.
  vcl.list
  200
  available       0 boot
  active          0 myconf3
  varnish> vcl.load conf1 default.vcl
  200
  VCL compiled.
  vcl.use conf1
  200
  VCL 'conf1' now active
  vcl.list
  200
  available       0 boot
  available       0 myconf3
  active          0 conf1
  三、后端wordpress主机配置
  wordpress的主要如下步骤,详细步骤不做赘述
  下载安装配置wordpress
  cp wp-config-sample.php wp-config.php
  数据库配置
  MariaDB [(none)]> create database wordpress;

  MariaDB [(none)]> grant all on wordpress.* to 'wpuser'@'172.16.%.%'>  MariaDB [(none)]> flush privileges;
  在动态主机的/var/www/html/下创建health.php用于动态健康状态检查
  DynamicServer is Health.
  在静态主机的/var/www/html/下创建health.html用于静态健康状态检查
  StaticServer is Health.
  四、测试
  对后端主机健康状态检查
  # varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
  200
  Varnish Cache CLI 1.0
  Linux,3.10.0-327.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
  varnish-4.0.5 revision 07eff4c29
  Type 'help' for command list.
  Type 'quit' to close CLI session.
  backend.list
  200
  Backend name            Refs   Admin      Probe
  default(127.0.0.1,,8080)       2      probe      Healthy (no probe)
  dynamic(172.16.1.10,,80)       1      probe      Healthy 3/3
  static(172.16.1.3,,80)      1      probe      Healthy 3/3
  客户端登陆keepalived生成的虚拟ip地址172.16.1.100
  第一次查看静态页面,未缓存X-Cache为miss
  blob.png
  刷新一下,缓存服务器就缓存了X-Cache为HIT
  blob.png
  查看动态页面
  blob.png
  登陆wordpress可以看到刚才正常上传图片的博客
  blob.png

页: [1]
查看完整版本: nginx varnish