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


    思路:

            servera:负载均衡服务器
            serverb:静态服务器
            serverc:动态服务器
            serverd:默认服务器

    servera(192.168.233.132):

    1. # 安装 Nginx 服务器
    2. yum install nginx -y
    3. #关闭防火墙和selinux
    4. systemctl stop firewalld
    5. setenforce 0
    6. # 切换到 Nginx 配置文件目录
    7. cd /etc/nginx/conf.d/
    8. # 使用 vim 编辑器打开 proxy.conf 文件进行编辑
    9. vim proxy.conf
    10. # 定义上传服务器的 upstream 块
    11. upstream uploadPools {
    12. server 192.168.233.140; # 后端服务器的IP地址
    13. }
    14. # 定义静态资源服务器的 upstream 块
    15. upstream staticPools {
    16. server 192.168.233.144; # 后端服务器的IP地址
    17. }
    18. # 定义主要应用服务器的 upstream 块
    19. upstream wwwPools {
    20. server 192.168.233.141; # 后端服务器的IP地址
    21. }
    22. server {
    23. listen 80; # 监听80端口
    24. server_name www.bbs.com; # 将请求转发到此服务器块中的指定域名
    25. # 处理静态资源请求
    26. location /static/ {
    27. proxy_pass http://staticPools; # 将请求转发到静态资源服务器
    28. proxy_set_header host $host; # 传递请求头信息
    29. proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
    30. }
    31. # 处理动态资源请求
    32. location /upload/ {
    33. proxy_pass http://uploadPools; # 将请求转发到上传服务器
    34. proxy_set_header host $host; # 传递请求头信息
    35. proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
    36. }
    37. # 默认处理所有其他请求
    38. location / {
    39. proxy_pass http://wwwPools; # 将请求转发到主要应用服务器
    40. proxy_set_header host $host; # 传递请求头信息
    41. proxy_set_header X-Forwarded-For $remote_addr; # 传递 X-Forwarded-For 头信息
    42. }
    43. }

    serverb(192.168.233.144):

    1. # 安装 Nginx 服务器
    2. yum install nginx -y
    3. #关闭防火墙和selinux
    4. systemctl stop firewalld
    5. setenforce 0
    6. # 切换到 Nginx 配置文件目录
    7. cd /etc/nginx
    8. # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
    9. vi nginx.conf
    10. # listen 80;
    11. # listen [::]:80;
    12. # root /usr/share/nginx/html;
    13. # 创建一个静态网页目录,并在其中创建一个简单的测试页面
    14. mkdir -p /data/web
    15. cd /data/web
    16. mkdir static
    17. echo static web test page > static/index.html
    18. # 切换到 Nginx 配置文件目录,并创建一个新的虚拟主机配置文件 www.conf,并添加配置项
    19. cd /etc/nginx/conf.d/
    20. vim www.conf
    21. # 添加以下内容:
    22. # server {
    23. # listen 80;
    24. # server_name www.bbs.com;
    25. # root /data/web/;
    26. # location / {
    27. # index index.html;
    28. # }
    29. # }
    30. # 重启 Nginx 服务器使配置生效
    31. systemctl restart nginx

    serverc(192.168.233.140):

    1. # 安装 Nginx 服务器
    2. yum install nginx -y
    3. #关闭防火墙和selinux
    4. systemctl stop firewalld
    5. setenforce 0
    6. # 切换到 Nginx 配置文件目录
    7. cd /etc/nginx
    8. # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
    9. vi nginx.conf
    10. # listen 80;
    11. # listen [::]:80;
    12. # root /usr/share/nginx/html;
    13. # 创建一个新的静态网页目录,并在其中创建一个简单的测试页面
    14. mkdir -p /data/web
    15. cd /data/web
    16. mkdir upload
    17. echo upload web test page > upload/index.html
    18. # 切换到 Nginx 配置文件目录,并创建一个新的虚拟主机配置文件 www.conf,并添加配置项
    19. cd /etc/nginx/conf.d/
    20. vim www.conf
    21. # 添加以下内容:
    22. # server {
    23. # listen 80;
    24. # server_name www.bbs.com;
    25. # root /data/web/upload;
    26. # location / {
    27. # index index.html;
    28. # }
    29. # }
    30. # 重启 Nginx 服务器使配置生效
    31. systemctl restart nginx

    serverd(192.168.233.141):

    1. # 安装 Nginx 服务器
    2. yum install nginx -y
    3. #关闭防火墙和selinux
    4. systemctl stop firewalld
    5. setenforce 0
    6. # 切换到 Nginx 配置文件目录
    7. cd /etc/nginx
    8. # 使用 vim 编辑器打开 nginx.conf 文件进行编辑,并注释掉 listen 和 root 配置项
    9. vi nginx.conf
    10. # listen 80;
    11. # listen [::]:80;
    12. # root /usr/share/nginx/html;
    13. # 创建一个新的静态网页目录,并在其中创建一个简单的测试页面
    14. mkdir -p /data/web
    15. echo "web test page." > /data/web/index.html
    16. # 使用 vim 编辑器打开 Nginx 的虚拟主机配置文件 www.conf,并添加配置项
    17. vi /etc/nginx/conf.d/www.conf
    18. # 添加以下内容:
    19. # server {
    20. # listen 80;
    21. # server_name www.bbs.com;
    22. # root /data/web/;
    23. # location / {
    24. # index index.html;
    25. # }
    26. # }
    27. # 重启 Nginx 服务器使配置生效
    28. systemctl restart nginx
    29. # 使用 vim 编辑器打开 /etc/hosts 文件,并添加 IP 地址和域名的映射关系
    30. vi /etc/hosts
    31. # 添加以下内容:
    32. # 192.168.233.132 www.bbs.com www.blog.com

    测试:

    1. # 使用 curl 命令访问 Nginx 服务器的默认主页
    2. curl http://192.168.233.132
    3. # 使用 curl 命令访问静态文件目录,并显示目录内容
    4. curl http://192.168.233.132/static/
    5. # 使用 curl 命令访问动态文件目录,并显示目录内容
    6. curl http://192.168.233.132/upload/


     

  • 相关阅读:
    C#编程语言在软件开发中的深度应用与实践
    利用 Kubernetes 降本增效?EasyMR 基于 Kubernetes 部署的探索实践
    Centos内网服务器搭建NTP时间同步服务
    数据结构--插入排序
    C++ 快速排序算法
    Mask-RCNN入门(balloon数据集、TensorFlow-DirectML)的N个坑
    webpack5学习进阶:Library、模块联邦、构建优化
    2022年浙江省考面试行政执法卷85分真题解析
    谁家分析数据还要开发啊,不都一键得报表吗?
    【JVM调优实战100例】03——JVM堆调优四例
  • 原文地址:https://blog.csdn.net/a17783481239/article/details/136138915