架构第七章:共享nfs和nginx负载均衡

发布于:2024-11-29 ⋅ 阅读:(11) ⋅ 点赞:(0)

环境:在第六章的分布式基础上搭建nfs
nginx1:192.168.8.10
mysql:192.168.8.20
php: 192.168.8.30

nfs: 192.168.8.40
nginx2: 192.168.8.50
nginx_lb: 192.168.8.60

1.搭建nfs共享服务器,把网站静态元素通过挂载方式放在nfs上

(1)开启一台centos7,安装nfs-utils、rpcbind:
yum -y install nfs-utils rpcbind

(2)创建挂载点
mkdir -p /nfs/blog

(3)发布共享目录
vim /etc/exports
添加:
/nfs/blog 192.168.8.0/24(rw,sync,no_root_squash)

保存退出

(4)重启nfs服务
systemctl restart rpcbind
systemctl restart nfs

(5)在nginx服务器上查看nfs共享目录
showmount -e 192.168.8.40

(6)把wordpress的内容目录挂载到nfs
cd /wordpress
cp -rp wp-content/ wp-contentbak
mount -t nfs 192.168.8.40:/nfs/blog wp-content
cp -rp wp-contentbak/* wp-content/

(7)设置永久挂载
vim /etc/fstab
添加:
192.168.8.40:/nfs/log /wordpress/wp-content nfs defaults,_netdev 0 0
保存退出

2.搭建第二台web服务器
(1)安装nginx
yum -y epel-release
yum -y install nginx

(2)把web1(192.168.8.10)上的nginx的配置复制到web2(192.168.8.50)
scp -rp /etc/nginx/* root@192.168.8.50:/etc/nginx

(3)把web1上网页源码复制到web2
scp -rp /wordpress root@192.168.8.50:/

(4)启动服务
systemctl start nginx

(5)php服务器修改配置文件,允许第二台nginx:
vim /etc/php-fpm.d/www.conf
修改:
listen.allowed_clients = 192.168.8.10,192.168.8.50
保存退出
systemctl restart php-fpm

3.搭建nginx负载均衡(192.168.8.60)
(1)安装nginx
yum -y install epel-release
yum -y install nginx

(2)配置负载均衡
vim /etc/nginx/conf.d/lb1.conf
添加:
upstream webcluster {
server 192.168.8.10:80;
server 192.168.8.50:80;
}
server {
listen 80;
server_name blog.benet.com;

    location / {
            proxy_pass      http://webcluster;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

保存退出
systemctl restart nginx

4.nginx1和nginx2,动态查看nginx访问日志
tail -f /var/log/nginx/access.log

5.客户端修改/etc/hosts,指向lb服务器,访问网站
vim /etc/hosts
192.168.8.60 blog.benet.com