• 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/


     

  • 相关阅读:
    猿创征文 第二季| #「笔耕不辍」--生命不息,写作不止#
    开了挂似的,直接涨薪25K,跪谢这份Java性能调优实战宝典
    谈谈TIME_WAIT
    使用python生成大量数据写入es数据库并查询操作2
    python -opencv 边缘检测
    Pandas数据分析:处理文本数据(str/object)各类操作+代码一文详解(一)
    美容小程序:让预约更简单,服务更贴心
    利用Cpolar内网穿透+HadSky技术构建您的私密高效论坛网站
    深度学习之基于Django+Tensorflow动物识别系统
    基于Python实现Eigenface人脸识别、特征脸识别
  • 原文地址:https://blog.csdn.net/a17783481239/article/details/136138915