Nginx负载均衡之按浏览器的不同来调度不同的后端节点

ip服务器功能
192.168.1.17Nginx Server处理请求
47.97.195.105(公网IP)
192.168.1.16
Proxy Server调度请求

配置WEB服务器

##这里是基于端口控制的,也可以设置成域名控制
[root@nginx conf.d]# vim phone.conf 
server {
        listen 8081;
        root /soft/app/iphone;

        location / {
                index index.html;
        }
}
server {
        listen 8082;
        root /soft/app/android;

        location / {
                index index.html;
        }
}
server {
        listen 8083;
        root /soft/app/chrome;

        location / {
                index index.html;
        }
}
server {
        listen 8084;
        root /soft/app/firefox;

        location / {
                index index.html;
        }
}

# 站点资源
[root@nginx /soft/app]# tree
.
├── android
│   └── index.html
├── chrome
│   └── index.html
├── firefox
│   └── index.html
└── iphone
    └── index.html

4 directories, 4 files
[root@nginx /soft/app]# 

# 查看端口是否启动
[root@nginx /soft/app]# netstat -lntp|grep nginx
tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      1235/nginx: master  
tcp        0      0 0.0.0.0:8082            0.0.0.0:*               LISTEN      1235/nginx: master  
tcp        0      0 0.0.0.0:8083            0.0.0.0:*               LISTEN      1235/nginx: master  
tcp        0      0 0.0.0.0:8084            0.0.0.0:*               LISTEN      1235/nginx: master  
[root@nginx /soft/app]# 

配置Nginx反向代理调度请求

[root@proxy /etc/nginx/conf.d]# vim proxy.conf

upstream iphone {
        server 192.168.1.17:8081;
}
upstream android {
        server 192.168.1.17:8082;
}
upstream chrome {
        server 192.168.1.17:8083;
}
upstream firefox {
        server 192.168.1.17:8084;
}

server {
        listen 80;
        server_name 47.97.195.105;
        root /usr/share/nginx/html;
        include /etc/nginx/conf.d/proxy_params;

        location / {
                if ($http_user_agent ~* "android"){
                        proxy_pass http://android;
                }

                if ($http_user_agent ~* "iphone"){
                        proxy_pass http://iphone;
                }

                if ($http_user_agent ~* "chrome"){
                        proxy_pass http://chrome;
                }

                if ($http_user_agent ~* "firefox"){
                        proxy_pass http://firefox;
                }
        }
}


[root@proxy /etc/nginx/conf.d]$ vim proxy_params 
proxy_redirect  default;
# 默认情况下,NGINX在代理请求中重新定义两个头字段“Host”和“Connection”
proxy_set_header Host $http_host;
# 远程的IP
proxy_set_header X-Real-IP $remote_addr;
# 远程的IP传递到X-Forwarded-For里,后端服务器获取X-Forwarded-For就可知道谁通过代理请求的
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 要防止将头字段传递给代理服务器,可将其设置为空字符串
proxy_set_header  Accept-Encoding  "";

不同浏览器访问测试

image

在这里插入图片描述
哈哈,没有iPhone手机

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值