ip | 服务器 | 功能 |
---|---|---|
192.168.1.17 | Nginx 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 "";
不同浏览器访问测试
哈哈,没有iPhone手机