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

配置WEB服务器

##这里是基于端口控制的,也可以设置成域名控制
[[email protected] 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;
        }
}

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

4 directories, 4 files
[[email protected] /soft/app]# 

# 查看端口是否启动
[[email protected] /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  
[[email protected] /soft/app]# 

配置Nginx反向代理调度请求

[[email protected] /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;
                }
        }
}


[[email protected] /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  "";

不同浏览器访问测试

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

Nginx负载均衡之按浏览器的不同来调度不同的后端节点
哈哈,没有iPhone手机

相关文章:

  • 2021-06-19
  • 2021-09-21
  • 2021-10-16
  • 2022-12-23
  • 2021-06-24
  • 2022-01-07
  • 2021-12-08
猜你喜欢
  • 2021-06-10
  • 2021-06-26
  • 2021-06-13
  • 2021-11-23
  • 2021-11-23
  • 2021-11-19
相关资源
相似解决方案