Nginx七层负载均衡之动静分离

news/2024/7/10 2:35:48 标签: nginx, 负载均衡, 运维

思路:

        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/


 


http://www.niftyadmin.cn/n/5380180.html

相关文章

【Linux内核】从0开始入门Linux Kernel源码

🌈 博客个人主页:Chris在Coding 🎥 本文所属专栏:[Linux内核] ❤️ 前置学习专栏:[Linux学习]从0到1 ⏰ 我们仍在旅途 ​ 目录 …

防火墙HA详细实验配置

下面是一个较为详细的防火墙HA实验配置示例,以VRRP作为HA协议为例。以下配置仅供参考,具体配置可能会根据防火墙设备型号和厂商的不同而有所差异。 1. 确保拥有两台防火墙设备(设备A和设备B),并设置基本配置&#xff…

给定n个结点m条边的简单无向图,判断该图是否存在鱼形状的子图:有一个环,其中有一个结点有另外两条边,连向不在环内的两个结点。若有,输出子图的连边

题目 思路&#xff1a; #include <bits/stdc.h> using namespace std; #define int long long #define pb push_back #define fi first #define se second #define lson p << 1 #define rson p << 1 | 1 const int maxn 1e6 5, inf 1e18 * 3, maxm 4e4 …

19-k8s的附加组件-coreDNS组件

一、概念 coreDNS组件&#xff1a;就是将svc资源的名称解析成ClusterIP&#xff1b; kubeadm部署的k8s集群自带coreDNS组件&#xff0c;二进制部署需要自己手动部署&#xff1b; [rootk8s231 ~]# kubectl get pods -o wide -A k8s系统中安装了coreDNS组件后&#xff0c;会有一个…

问题记录——c++ sort 函数 和 严格弱序比较

引出 看下面这段cmp函数的定义 //按照vector第一个元素升序排序 static bool cmp(const vector<int>& a, const vector<int>& b){return a[0] < b[0]; }int eraseOverlapIntervals(vector<vector<int>>& intervals) {//按区间左端排序…

【Hadoop】Yarn 任务管理指令

列出所有Application : yarn application -list # 可以根据状态过滤&#xff1a;ALL、NEW、NEW_SAVING、SUBMITTED、ACCEPTED、RUNNING、FINISHED、FAILED、KILLED yarn application -list -appStates FINISHED application_1652832924022_57402 DDOS-Detect-Engine_Ver000 …

【Webpack】CSS 处理

CSS 处理 提取 Css 成单独文件 Css 文件目前被打包到 js 文件中&#xff0c;当 js 文件加载时&#xff0c;会创建一个 style 标签来生成样式 这样对于网站来说&#xff0c;会出现闪屏现象&#xff0c;用户体验不好 我们应该是单独的 Css 文件&#xff0c;通过 link 标签加载…

【ChatIE】论文解读:Zero-Shot Information Extraction via Chatting with ChatGPT

文章目录 介绍ChatIEEntity-Relation Triple Extration (RE)Named Entity Recognition (NER)Event Extraction (EE) 实验结果结论 论文&#xff1a;Zero-Shot Information Extraction via Chatting with ChatGPT 作者&#xff1a;Xiang Wei, Xingyu Cui, Ning Cheng, Xiaobin W…