一.nginx负载均衡
准备环境
准备三台虚拟机,一台做代理,两台做web服务器。(在这里我的代理服务器用的是编译安装的nginx,两台web用的是yum安装的nginx)
开机,关闭防火墙和selinux。
保证网络畅通,可以ping通百度。
启动nginx:
- 编译安装绝对路径启动:/usr/local/nginx/sbin/nginx。也可以添加全局变量,在/etc/profile.d/下创建一个脚本(nginx.sh),将export PATH=${PATH}:/usr/local/nginx/sbin/添加到脚本中,想要立刻生效可以source一下脚本。
- yum安装:systemctl start nginx
配置web服务器
其实web服务器没有什么要配置的,但为了最后显示的结果更易懂,我们在这里将nginx默认文件发布目录下的内容稍微修改一下。
- 在nginx的配置文件中查看网站的默认发布目录
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf`
- 两台web服务器都可以这样查找
- 服务器就配置完了
配置代理服务器
打开配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf
在server上面创建一个upstream,名字自定义(在这里我起的是web1)
找到location,注释掉前两行,添加以下代码
proxy_pass http://web1;
proxy_redirect default;
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;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://web1;
proxy_redirect default;
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;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
}
将添加的upstream的名称修改到以下位置
退出,重新加载配置文件
[root@localhost ~]# nginx -s reload
访问代理服务器
[root@localhost ~]# curl 192.168.49.144
二.添加nfs
关闭防火墙和selinux
服务器端配置nfs
- 安装nfs
[root@localhost ~]# yum -y install nfs-utils
- 启动nfs
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs-server
- 创建一个目录并在目录下创建一个文件,写入内容。
[root@localhost ~]# mkdir /data
[root@localhost ~]# vim /data/index.html
- 共享这个目录下的文件
[root@localhost ~]# vim /etc/exports
- 添加以下内容
/data/index.html *(ro,sync)
- 重新刷新共享
[root@localhost ~]# exportfs -rv
客户端配置(两台web服务器)
- 安装nfs
[root@localhost ~]# yum -y install nfs-utils
- 查看存储端共享
[root@localhost ~]# showmount -e 192.168.49.145
- 分别在两台服务器上创建目录并挂载到共享目录上
[root@localhost ~]# mkdir -p /mnt/nfs1
[root@localhost ~]# vim /etc/fstab
192.168.49.145:/data/ /mnt/nfs1 nfs defaults 0 0
[root@localhost ~]# mount -a
- 修改web服务器的网站发布目录,改为共享挂载的目录
- 重新加载配置文件
[root@localhost ~]# nginx -s reload
- 访问自己的ip
[root@localhost ~]# curl 192.168.49.143
- 这时访问代理服务器就会得到nfs客户端下index.html里面的内容