Nginxproxy+varnish

Nginx负载均衡它可基于四层和七层之间进行调度,而且对后端各个节点具有健康状态检测的功能,当后端服务器故障时,会自动标记为不可用状态,当后端节点上线时再将请求调度至此节点,配合keepalived利用飘移IP,高可用保证调度器的实时在线。为保证热区数据的快速响应配合varnish缓存加速,并实现动静分离。

案例架构拓扑如下:

Nginx LB+keepalived+varnish+NAMP架构部署wordpress实现动静分离_第1张图片

拓扑环境如下:

前端:Nginx+keepalived高可用

调度服务器:Varnish1、Varnish2

缓存服务器:Varnish

1、VarnishServer两台
 2、动静分离后端服务器,并动静都提供负载均衡效果

后端服务器:

NginxStaticWebServer+NFS-client
 AMP DynamicWebServer+NFS-server

1.安装nginx和keepalived后做keepalived配置如下:

! Configuration File for keepalived

global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id  node2
   vrrp_mcast_group4 224.0.234.123

}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 36
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 335566
    }
    virtual_ipaddress {
        192.168.1.98/24 dev eth0                                                                                      
    }                                                                                                                                                                                     
}                                                    
vrrp_instance VI_2 {                                            
    state BACKUP                                
    interface eth0                                    
    virtual_router_id 37                                        
    priority 99                   
    advert_int 1                                      
    authentication {                                   
        auth_type PASS                               
        auth_pass 33556677                           
    }                                                   
    virtual_ipaddress {                              
        192.168.1.99/24 dev eth0                             
    }                                                                                        
}                                                 
#notify_master "/etc/keepalived/notify.sh master"     #notify_backup "/etc/keepalived/notify.sh backup"
#notify_fault "/etc/keepalived/nofify.sh fault"     
}
  1. Nginx负载均衡简单配置如下:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
stream {
        upstream  nasrvs {
                server 192.168.5.107:80;
                server 192.168.5.108:80;
        }
        server {
                listen 80;
                proxy_pass nasrvs;
                proxy_timeout 60s;
                proxy_connect_timeout 10s;
        }
}

另一台Nginx负载均衡及keepalived基本配置相同更改相应的参数即可。

3.后端Varnish-server配置如下:

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "192.168.20.109";
    .port = "80";
}
backend nginxsrvs {
  .host = "192.168.20.108";
   .port = "80";
}

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
    if (req.method == "PURGE") {
         return(purge);
}
    if (req.url ~ "(?i)\.(css|svg|js|jpg|jpeg|png|gif|pdf)") {
         set req.backend_hint = nginxsrvs;
    } else {
         set req.backend_hint =   default;
    }

}

sub vcl_purge {
        return (synth(200,"Purged"));
}

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;                                                              
    }

两台varnishserver的配置基本一致,更改相应的参数即可。

4.AMP将httpd,mariadb-server,php-mysql,php安装后httpd的配置如下:


        DirectoryIndex index.php index.html
        ServerName www.abc.com
        DocumentRoot /apps/data/wordpress
        
                Options FollowSymLinks
                AllowOverride None
                Require all granted
        

5.创建/apps/data目录后将wordpress的程序文件解压缩至此目录在mariadb中创建库及账号,此前文章有介绍到wordpress的部署方法。并配置nfs编辑/etc/exports文件如下:

/apps/data *(rw,all_squash,anonuid=48)

执行exportfs  -ar导出所有目录

6.配置Nginx static web server配置如下:

server {
    listen       80;
    server_name  www.abc.com;
    location / {
        root   /apps/data/wordpress;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}                                                  
}

7.NFS-client的配置将NFS-server的导出目录挂载至/apps/data编辑/etc/fstab:

192.168.20.109:/apps/data /apps/data nfs rw,defaults 0 0

整个配置完成做一下测试访问:

Nginx LB+keepalived+varnish+NAMP架构部署wordpress实现动静分离_第2张图片

以上内容还有待完善。