Web集群案例实战 -- Nginx 反向代理根据URL中的目录地址实现代理转发 -- 案例实战

Nginx 反向代理根据URL中的目录地址实现代理转发 -- 案例实战

前言

本环境是基于 Centos 7.8 系统构建Nginx学习环境
具体构建,请参考 Nginx-1.18.0 环境部署


一、需求背景

需求背景

通过Nginx实现动静分离,即通过Nginx反向代理配置规则实现让动态资源和静态资源及其他业务分别由不同的服务器解析,以解决网站性能,安全,用户体验等重要问题。

应用场景

  • 根据HTTP的URL进行转发的应用情况,被称为第7层(应用层)的负载均衡,而LVS的负载均衡一般用于TCP等的转发,因此被称为第4层(传输层)的负载均衡。
  • 在企业中,有时希望只用一个域名对外提供服务,不希望使用多个域名对应同一个产品业务,此时就需要在代理服务器上通过配置规则,使得匹配不同规则的请求会交给不同的服务器池处理。
  • 业务的域名没有拆分或者不希望拆分,但希望实现动静分离,多业务分离,这在前面已经讲解过案例了。
  • 不同的客户端设备(例如:手机和PC端)使用同一个域名访问同一个业务网站,就需要根据规则将不同设备的用户请求交给后端不同的服务器处理,以便得到最佳用户体验

设计架构
在这里插入图片描述

环境准备

rolehostipnginx-versionOS
nginx proxy hostnode01192.168.5.11Nginx-1.18.0Centos 7.8
nginx web server1node02192.168.5.12Nginx-1.18.0Centos 7.8
nginx web server2node03192.168.5.13Nginx-1.18.0Centos 7.8
nginx web server3node04192.168.5.13Nginx-1.18.0Centos 7.8
nginx client1node05192.168.5.15----Centos 7.8
nginx client2windows 7 Ultimate.192.168.5.7----windows 7 Ultimate.

配置后端 web服务

---node02 
[root@node02 ~]# mkdir /usr/share/nginx/html/static/
[root@node02 ~]# echo static_pools > /usr/share/nginx/html/static/index.html
[root@node02 ~]# vim /etc/nginx/conf.d/vhost.conf 
server {
    listen       80;
    server_name  static.wan.com;
    location / {
        root   /usr/share/nginx/html/static;
        index  index.html index.htm;
    }
}
[root@node02 ~]# systemctl restart nginx

---node03
[root@node03 ~]# mkdir /usr/share/nginx/html/upload/
[root@node03 ~]# echo upload_pools > /usr/share/nginx/html/upload/index.html
[root@node03 ~]# vim /etc/nginx/conf.d/vhost.conf
server {
    listen       80;
    server_name  upload.wan.com;
    location / {
        root   /usr/share/nginx/html/upload;
        index  index.html index.htm;
    }
}
[root@node03 ~]# systemctl restart nginx

---node04
[root@node04 ~]# mkdir /usr/share/nginx/html/default/
[root@node04 ~]# echo default_pools > /usr/share/nginx/html/default/index.html
[root@node04 ~]# vim /etc/nginx/conf.d/vhost.conf 
server {
    listen       80;
    server_name  default.wan.com;
    location / {
        root   /usr/share/nginx/html/default;
        index  index.html index.htm;
    }
}
[root@node04 ~]# systemctl restart nginx

配置proxy host

[root@node01 ~]# vim /etc/nginx/conf.d/vhost.conf 
upstream static_pools {
     server 192.168.5.12 weight=1;
     }

upstream upload_pools {
     server 192.168.5.12 weight=1;
     }

upstream default_pools {
     server 192.168.5.12 weight=1;
     }


server {
    listen       80;
    server_name  web.wan.org;

location /static {
    proxy_pass http://static_pools;
    proxy_set_header X-Forwarded-For $remote_addr;
    }

location /default {
    proxy_pass http://default_pools;
    proxy_set_header X-Forwarded-For $remote_addr;
    }

location /upload {
    proxy_pass http://upload_pools;
    proxy_set_header X-Forwarded-For $remote_addr;
    }
}
[root@node01 ~]# systemctl restart nginx

node05测试

添加hosts解析

[root@node05 ~]# vim /etc/hosts
192.168.5.11 web.wan.org

在这里插入图片描述
windows 7 Ultimate. 测试

添加hosts解析
在这里插入图片描述
浏览器访问:http://web.wan.org/static/
在这里插入图片描述
浏览器访问:http://web.wan.org/upload/
在这里插入图片描述

浏览器访问:http://web.wan.org/default/
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值