思路:
servera:负载均衡服务器
serverb:静态服务器
serverc:动态服务器
serverd:默认服务器
servera(192.168.233.132):
- # 安装 Nginx 服务器
- yum install nginx -y
-
- #关闭防火墙和selinux
- systemctl stop firewalld
- setenforce 0
-
- # 切换到 Nginx 配置文件目录
- cd /etc/nginx/conf.d/
-
- # 使用 vim 编辑器打开 proxy.conf 文件进行编辑
- vim proxy.conf
-
- # 定义上传服务器的 upstream 块
- upstream uploadPools {
- server 192.168.233.140; # 后端服务器的IP地址
- }
-
- # 定义静态资源服务器的 upstream 块
- upstream staticPools {
- server 192.168.233.144; # 后端服务器的IP地址
- }
-
- # 定义主要应用服务器的 upstream 块
- upstream wwwPools {
- server 192.168.233.141; # 后端服务器的IP地址
- }
-
- server {
- listen 80; # 监听80端口
- server_name www.bbs.com; # 将请求转发到此服务器块中的指定域名
-
- # 处理静态资源请求
- location /static/ {
- proxy_pass http://staticPools; # 将请求转发到静态资源服务器
- proxy_set_header host $host; # 传递请求头信息
- proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
- }
-
- # 处理动态资源请求
- location /upload/ {
- proxy_pass http://uploadPools; # 将请求转发到上传服务器
- proxy_set_header host $host; # 传递请求头信息
- proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
- }
-
- # 默认处理所有其他请求
- location / {
- proxy_pass http://wwwPools; # 将请求转发到主要应用服务器
- proxy_set_header host $host; # 传递请求头信息
- proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
- }
- }
serverb(192.168.233.144):
- # 安装 Nginx 服务器
- yum install nginx -y
-
- #关闭防火墙和selinux
- systemctl stop firewalld
- setenforce 0
-
- # 切换到 Nginx 配置文件目录
- cd /etc/nginx
-
- # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
- vi nginx.conf
- # listen 80;
- # listen [::]:80;
- # root /usr/share/nginx/html;
-
- # 创建一个静态网页目录,并在其中创建一个简单的测试页面
- mkdir -p /data/web
- cd /data/web
- mkdir static
- echo static web test page > static/index.html
-
- # 切换到 Nginx 配置文件目录,并创建一个新的虚拟主机配置文件 www.conf,并添加配置项
- cd /etc/nginx/conf.d/
- vim www.conf
- # 添加以下内容:
- # server {
- # listen 80;
- # server_name www.bbs.com;
- # root /data/web/;
- # location / {
- # index index.html;
- # }
- # }
-
- # 重启 Nginx 服务器使配置生效
- systemctl restart nginx
serverc(192.168.233.140):
- # 安装 Nginx 服务器
- yum install nginx -y
-
- #关闭防火墙和selinux
- systemctl stop firewalld
- setenforce 0
-
- # 切换到 Nginx 配置文件目录
- cd /etc/nginx
-
- # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
- vi nginx.conf
- # listen 80;
- # listen [::]:80;
- # root /usr/share/nginx/html;
-
- # 创建一个新的静态网页目录,并在其中创建一个简单的测试页面
- mkdir -p /data/web
- cd /data/web
- mkdir upload
- echo upload web test page > upload/index.html
-
- # 切换到 Nginx 配置文件目录,并创建一个新的虚拟主机配置文件 www.conf,并添加配置项
- cd /etc/nginx/conf.d/
- vim www.conf
- # 添加以下内容:
- # server {
- # listen 80;
- # server_name www.bbs.com;
- # root /data/web/upload;
- # location / {
- # index index.html;
- # }
- # }
-
- # 重启 Nginx 服务器使配置生效
- systemctl restart nginx
serverd(192.168.233.141):
- # 安装 Nginx 服务器
- yum install nginx -y
-
- #关闭防火墙和selinux
- systemctl stop firewalld
- setenforce 0
-
- # 切换到 Nginx 配置文件目录
- cd /etc/nginx
-
- # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
- vi nginx.conf
- # listen 80;
- # listen [::]:80;
- # root /usr/share/nginx/html;
-
- # 创建一个新的静态网页目录,并在其中创建一个简单的测试页面
- mkdir -p /data/web
- echo "web test page." > /data/web/index.html
-
- # 使用 vim 编辑器打开 Nginx 的虚拟主机配置文件 www.conf,并添加配置项
- vi /etc/nginx/conf.d/www.conf
- # 添加以下内容:
- # server {
- # listen 80;
- # server_name www.bbs.com;
- # root /data/web/;
- # location / {
- # index index.html;
- # }
- # }
-
- # 重启 Nginx 服务器使配置生效
- systemctl restart nginx
-
- # 使用 vim 编辑器打开 /etc/hosts 文件,并添加 IP 地址和域名的映射关系
- vi /etc/hosts
- # 添加以下内容:
- # 192.168.233.132 www.bbs.com www.blog.com
-
测试:
- # 使用 curl 命令访问 Nginx 服务器的默认主页
- curl http://192.168.233.132
-
- # 使用 curl 命令访问静态文件目录,并显示目录内容
- curl http://192.168.233.132/static/
-
- # 使用 curl 命令访问动态文件目录,并显示目录内容
- curl http://192.168.233.132/upload/
-